May I request for a new feature?

When I wrote a scala script, such as this simple logic:

$ cat MyTest.sc
println("testing!")

It can’t be complied by scalac due to no entry func exists:

$ scalac MyTest.sc
MyTest.sc:1: error: expected class or object definition

Is there a way that when I call scalac above, scalac translates the script automatically to the program below:

$ cat MyTest.scala 
object MyTest { // auto enclosed in a object
   def main(args:Array[String]):Unit = { // auto generating a main func

    println("testing!")

   }
}

After then I can run the compiling:

$ scalac MyTest.scala 

for example, when calling this syntax:

$ scalac --a-new-option MyTest.sc

scalac will try to compile everything in the digital world, even if it’s a bash script.

Thank you.

The Ammonite scripts do just this very well - for a quick run without setup I always use the Ammonite REPL and/or scripts.

regards,
Siddhartha

1 Like

You don’t specify which version of Scala, as the tooling is entirely different for Scala 3.

As shown in my previous answer, for Scala 2

➜  scala -Xscript hi hello.sc
hello, world!
➜  scalac -Xscript hi hello.sc

where the arg names the wrapper class.

Scala 3 are still working on feature parity, or perhaps scala-cli will provide solutions.

1 Like

Many thanks!

This is pretty cute. I never know this. Thanks.

There is a list of options which I happen to have handy, but it’s hard to know where to start.

I don’t think I’ve ever specified my own -Xsource-reader and the FOMO is killing me.