0

I am really sorry, this seems basic. I am using Programming in Scala and I have got to step 4:

Put this into a file named hello.scala:

@main def m():
        println("Hello world from a script!"

At first I got an illegal start of definition error, since pivoting to using vs code I get the following:

(base) *dir* scala % scala hello.scala
Compiling project (Scala 3.5.1, JVM (21))
[error] ./hello.scala:2:12
[error] end of toplevel definition expected but '(' found
[error]     println("hello world from script")
[error]            ^
[error] ./hello.scala:1:11
[error] Declaration of method m not allowed here: only classes can have declared but undefined members
[error] @main def m():
[error]           ^
Error compiling project (Scala 3.5.1, JVM (21))

I thought it might be a new line issue, so I moved everything onto one line like @main def m(): println("hello world from script") and I get the following:

(base) *dir* scala % scala hello.scala
Compiling project (Scala 3.5.1, JVM (21))
[error] ./hello.scala:1:23
[error] end of toplevel definition expected but '(' found
[error] @main def m(): println("hello world from script")
[error]                       ^
[error] ./hello.scala:1:11
[error] Declaration of method m not allowed here: only classes can have declared but undefined members
[error] @main def m(): println("hello world from script")
[error]           ^
Error compiling project (Scala 3.5.1, JVM (21))

Using scalac hello.scala does not work either.

3
  • 1
    You are missing an = methods don't start with :, rather : is used to specify the return type. Commented Nov 18, 2024 at 23:03
  • What happens if you replace the : with =? Commented Nov 18, 2024 at 23:04
  • Thank you @LuisMiguelMejíaSuárez Commented Nov 19, 2024 at 14:14

1 Answer 1

1

You need to keep something like this in the file:

object HelloWorld {
  def main(args: Array[String]): Unit = {
    println("Hello, World!")
  }
}

Then save that file say hello.scala

from same folder in terminal

scalac HelloWorld.scala

then

scala HelloWorld

Another way is to do a script like

@main def m(): Unit =
  println("Hello, world from a script!")

This you can run directly by calling

scala hello.scala

Its great to read a book but might help to also watch a few videos that have screen share on so can see the actions and maybe get some code ~ search like this

  1. https://github.com/Baeldung/scala-tutorials
  2. https://github.com/jetbrains-academy/scala-tutorial
  3. https://github.com/deanwampler/spark-scala-tutorial
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for the answer and the resources, apologies I knew it was a very simple question
Np if it helped mark the answer as accepted

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.