0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Scala CLIでミニマムにスクリプトを動かしてみる

Last updated at Posted at 2024-06-11

ミニマムな例

以下はHello World的なプログラムとその実行の様子です。
1行のスクリプトですが、sbtで普通に実行するのと比べて、コンパイルが体感かなり早いです。

// main.scala
@main def program = println("hello, Scala CLI!")

初回実行:
h.gif

標準出力内容を少し変えて再実行:
k.gif

コマンドライン引数の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"
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?