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.
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