Scalaの人材がいないっていうつぶやきを見て、久しぶりに触ってみるやり取りになったので、実際に始めてみた。
自分が触ってた最後の年が2014年なので2.11か。
いまは3.4.2らしい。
入門 | Scala Documentation
公式に行ったら、ずいぶんと親切になっててびっくり。
無事にsbtのinstallもできたのでVSCode +metalを利用して一旦起動。
昔使ってたIntelliJのディレクトリだけ残ってたのでそこにsbtプロジェクトを作成。
今の会社がVSCodeなのでIntelliJのライセンス更新やめちゃったんだよな...
それだけ自分磨きをサボっていたということなので、反省しつつ一旦はVSCodeで進める。
coursierのPATHを通してなかったのでsbtコマンドが見つからないという初心者ムーブをかましつつ
```
$ export PATH="$PATH:/Users/sakamotomitsuru/Library/Application Support/Coursier/bin"
$ export PATH="$PATH:/Users/sakamotomitsuru/Library/Application Support/sbt/bin"
```
sbtコマンドが動くのを確認。
```
$ sbt
WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by org.jline.terminal.impl.exec.ExecTerminalProvider$ReflectionRedirectPipeCreator (file:/Users/xxxxxxx/.sbt/boot/scala-2.12.19/org.scala-sbt/sbt/1.10.1/jline-terminal-3.24.1.jar) to constructor java.lang.ProcessBuilder$RedirectPipeImpl()
WARNING: Please consider reporting this to the maintainers of org.jline.terminal.impl.exec.ExecTerminalProvider$ReflectionRedirectPipeCreator
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release
[info] welcome to sbt 1.10.1 (Oracle Corporation Java 12.0.1)
[info] loading settings for project hello-world-build-build from metals.sbt ...
[info] loading project definition from /Users/sakamotomitsuru/IdeaProjects/scala3/hello-world/project/project
[info] loading settings for project hello-world-build from metals.sbt ...
[info] loading project definition from /Users/xxxxxxxx/IdeaProjects/scala3/hello-world/project
[success] Generated .bloop/hello-world-build.json
[success] Total time: 2 s, completed 2024/07/26 23:44:52
[info] loading settings for project root from build.sbt ...
[info] set current project to hello-world (in build file:/Users/sakamotomitsuru/IdeaProjects/scala3/hello-world/)
[info] sbt server started at local:///Users/xxxxx/.sbt/1.0/server/002700689fa0a333c4e0/sock
[info] started sbt server
sbt:hello-world> run
[info] running hello
Hello world!
I was compiled by Scala 3. :)
[success] Total time: 0 s, completed 2024/07/26 23:45:04
```
動かしてみて気がつく。
sbtの起動ログに`Oracle Corporation Java 12`っていうのがある。
普段開発していない環境だというのがよく分かる。
Java更新はされないのねと思いつつopenjdkのパスを通してJavaのバージョンが上がったことを確認
```
$ echo 'export PATH="/usr/local/opt/openjdk/bin:$PATH"' >> ~/.zshrc
$ echo 'export CPPFLAGS="-I/usr/local/opt/openjdk/include"' >> ~/.zshrc
$ source ~/.zshrc
```
```
$ java --version
openjdk 22.0.2 2024-07-16
OpenJDK Runtime Environment Homebrew (build 22.0.2)
OpenJDK 64-Bit Server VM Homebrew (build 22.0.2, mixed mode, sharing)
```
もう一度HelloWorldにトライ。
```
$ sbt
copying runtime jar...
[info] welcome to sbt 1.10.1 (Homebrew Java 22.0.2)
[info] loading settings for project hello-world-build-build from metals.sbt ...
[info] loading project definition from /Users/xxxxxxxxx/IdeaProjects/scala3/hello-world/project/project
[info] loading settings for project hello-world-build from metals.sbt ...
[info] loading project definition from /Users/xxxxxxxxx/IdeaProjects/scala3/hello-world/project
[success] Generated .bloop/hello-world-build.json
[success] Total time: 1 s, completed 2024/07/26 23:52:57
[info] loading settings for project root from build.sbt ...
[info] set current project to hello-world (in build file:/Users/xxxxxxxxx/IdeaProjects/scala3/hello-world/)
[info] sbt server started at local:///Users/xxxxxxxxx/.sbt/1.0/server/002700689fa0a333c4e0/sock
[info] started sbt server
sbt:hello-world> run
[info] compiling 1 Scala source to /Users/xxxxxxxx/IdeaProjects/scala3/hello-world/target/scala-3.4.2/classes ...
[info] running hello
Hello world!
I was compiled by Scala 3. :)
[success] Total time: 3 s, completed 2024/07/26 23:53:12
sbt:hello-world>
```
ああ、なんかそれっぽくなった。今日はここまで。
明日はそのまま 入門 | Scala Documentation の 「次のステップ」 に進むことにする。
最終ゴールとしては、Cloud Runにアプリケーションをデプロイしたいものがあるのでそこまでやる。