3

I want to execute a following script using Scala3:

@main def m() =
  println("Hello, world! I'm a script")

When I type the command, scala hello.scala, I get the following error:

/Users/avirals/dev/learning-scala/hello-world/hello.scala:1: error: not found: type main
@main def m() =
 ^
one error found

I think it is because I have both the versions of Scala installed (2 and 3). I know how to start a REPL for both (as mentioned here) but I am not able to execute a Scala3 script from the command line.

[Update]

I tried scala3-repl hello.scala and it just opens the REPL:

➜  learning-scala git:(main) scala3-repl hello.scala
scala> m()
1 |m()
  |^
  |Not found: m

How do I execute a Scala 3 script from the command line given I have two different versions (2 and 3) of Scala installed?

My OS: MacOS

Update 2

As suggested in this answer, I tried running with amm and it worked for a few scripts. However, the following script failed:

Script:

@main def m(args: String*) =
  var i = 0
  while i < args.length do
    println(args(i))
    i += 1

Error:

➜  learning-scala git:(main) amm printargs.scala
printargs.scala:2:3 expected (If | While | Try | DoWhile | For | Throw | Return | ImplicitLambda | SmallerExprOrLambda)
  var i = 0
  ^

Running the above script in a Scala3-REPL works:

➜  learning-scala git:(main) scala3-repl
scala> @main def m(args: String*) =
     |   var i = 0
     |   while i < args.length do
     |     println(args(i))
     |     i += 1
     |
def m(args: String*): Unit

scala> m("aviral", "srivastava")
aviral
srivastava

Running the same script in a system (MacOS) that has only Scala3 installed works just fine as well.

4
  • 1
    What is the "system"? If you are trying to find out how to execute specific programs on your system, you should consult the documentation for that system. What is it, windows, macos, some linux distro? How is it even a scala question? Commented May 5, 2021 at 22:14
  • @AndreyTyukin I updated the OS, it is MacOS. Commented May 5, 2021 at 22:25
  • 1
    @jwvh I updated the answer, it does not work. Commented May 5, 2021 at 22:25
  • scala-cli.virtuslab.org Commented Sep 29, 2022 at 13:48

1 Answer 1

3

There exists currently Minimal scripting support #11379. I was able to get it working by manually downloading a release from https://github.com/lampepfl/dotty/releases/download/3.0.0-RC3/scala3-3.0.0-RC3.zip, unzipping, and giving executable permission to launchers

./scala3-3.0.0-RC3/bin/scala hello.scala
Hello, world! I'm a script

With scala3-repl launcher you could at least do

$ scala3-repl
scala> :load hello.scala                                                                                                                                                                                      
def m(): Unit

scala> m()                                                                                                                                                                                                  
Hello, world! I'm a script
Sign up to request clarification or add additional context in comments.

3 Comments

Why did it work? How did amm figure out Scala 3 is needed here? I did not specify anything pertaining to Scala versions anywhere. Thanks!
@AviralSrivastava Sorry ammonite currently does not work but you can subscribe to the open pull request github.com/com-lihaoyi/Ammonite/pull/1150 and watch progress.
No, it did work for a bit but then failed for complex scripts ( scripts involving functions and for loops and stuff).

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.