ミニマムな例
以下はHello World的なプログラムとその実行の様子です。
1行のスクリプトですが、sbtで普通に実行するのと比べて、コンパイルが体感かなり早いです。
// main.scala
@main def program = println("hello, Scala CLI!")
コマンドライン引数のjsonをフォーマットする
circeなどの外部ライブラリを使用することも可能みたいです。
jsonfmt.scala
//> using scala "3.4.0"
//> using dep "io.circe::circe-core::0.14.7"
//> using dep "io.circe::circe-parser::0.14.7"
import io.circe.*, io.circe.parser.*
@main def program(json: String) =
parse(json) match
case Right(j) =>
println(j)
case error =>
println(error)
実行結果:
$ scala-cli jsonfmt.scala -- '{"foo": "bar"}'
{
"foo" : "bar"
}