簡體   English   中英

當系統同時安裝了 Scala2 和 Scala3 時如何執行 Scala3 腳本?

[英]How to execute a Scala3 script when system has both Scala2 and Scala3 installed?

我想使用 Scala3 執行以下腳本:

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

當我輸入命令scala hello.scala ,我收到以下錯誤:

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

我認為這是因為我安裝了 Scala 的兩個版本(2 和 3)。 我知道如何為兩者啟動 REPL(如此所述),但我無法從命令行執行 Scala3 腳本。

[更新]

我試過scala3-repl hello.scala它只是打開了REPL:

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

鑒於我安裝了 Scala 的兩個不同版本(2 和 3),如何從命令行執行 Scala 3 腳本?

我的操作系統:MacOS

更新 2

正如這個答案中所建議的,我嘗試使用amm運行,它適用於一些腳本。 但是,以下腳本失敗:

腳本:

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

錯誤:

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

在 Scala3-REPL 中運行上述腳本有效:

➜  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

在僅安裝了 Scala3 的系統 (MacOS) 中運行相同的腳本也可以正常工作。

當前存在最小腳本支持#11379 我可以通過從https://github.com/lampepfl/dotty/releases/download/3.0.0-RC3/scala3-3.0.0-RC3.zip手動下載版本,解壓縮並提供可執行文件來使其工作發射器許可

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

使用scala3-repl啟動器,您至少可以做到

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

scala> m()                                                                                                                                                                                                  
Hello, world! I'm a script

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM