Home > Enterprise >  Getting syntax error for println in scala
Getting syntax error for println in scala

Time:11-08

I'm trying to run the following code but getting an error on Scala 3.0.0:

// saved as file Upper1.scala

class Upper1:
  def convert(strings: Seq[String]): Seq[String] =
    strings.map((s: String) => s.toUpperCase)

val up = new Upper1()
val uppers = up.convert(List("Hello", "World!"))
println(uppers)
% scala Upper1.scala 
-- [E080] Syntax Error: /Users/Deb/Desktop/Temp/Upper1.scala:7:0 ------------
7 |println(uppers)
  |^^^^^^^
  |Expected a toplevel definition
1 error found
Error: Errors encountered during compilation

Am I running it incorrectly?

CodePudding user response:

At the top-level, you can write only definition (e.g., val, def, class, ...) but not expression (e.g., 1 1, or in general functional call).

Therefore, if you wanna execute println(uppers) during the execution, you can write the main of the program:

@main
def main(): Unit =
  println(uppers)

  • Related