scala tips

fishをlogin shellに設定する

scalaはhttps://get-coursier.io/docs/cli-overview#install
これがパッケージ管理ツールかな

csでinstallしたコマンドを叩けるようにパスを通す
eval "$(cs install --env)"
fishでも聞いた

公式はこれ

sbt new scala/scala3.g8
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
A template to demonstrate a minimal Scala 3 application
なんか出てきた。
template自体はできたっぽい

build?最初の環境構築は結構重い印象

とりあえず個人ブログとかweb関連はscalaでやっていきたいわね

sbt > ~runがホットリロードも兼ね備えてくてるのね。ええやん

ここのコンパイルは結構早い印象

補完で型出してくれるんいいわー

一通り文法見ていこうかしら

val someList = List(1,2,3,4, "hello")
someList.map(_ * 3)
[info] Build triggered by /Users/yuunag1/workspace/scala-helloworld/hello-wiorld/src/main/scala/Main.scala. Running 'run'.
[info] compiling 1 Scala source to /Users/yuunag1/workspace/scala-helloworld/hello-wiorld/target/scala-3.3.1/classes ...
[error] -- [E008] Not Found Error: /Users/yuunag1/workspace/scala-helloworld/hello-wiorld/src/main/scala/Main.scala:9:17
[error] 9 | someList.map(_ * 3)
[error] | ^^^
[error] | value * is not a member of Int | String
[error] one error found
[error] (Compile / compileIncremental) Compilation failed
[error] Total time: 1 s, completed Oct 4, 2023, 9:21:45 PM
[info] 5. Monitoring source files for root/run...
[info] Press <enter> to interrupt or '?' for more options.
~runを実行したままコードを書くとその場でinterpreterも動く。
型つきであることに加えてかなり早いinterpreterを備えてるのは素直にすごいと思う
今回の場合、caseとかで振り分けたりっていうの意をメソッドチェーン使ってできたりしないのかな

case class Person(name: String)
val p = Person("nilou")
println(p.name)
p.name = "nahida"
info] compiling 1 Scala source to /Users/yuunag1/workspace/scala-helloworld/hello-wiorld/target/scala-3.3.1/classes ...
[error] -- [E052] Type Error: /Users/yuunag1/workspace/scala-helloworld/hello-wiorld/src/main/scala/Main.scala:14:9
[error] 14 | p.name = "nahida"
[error] | ^^^^^^^^^^^^^^^^^
[error] | Reassignment to val name
[error] |
[error] | longer explanation available when compiling with `-explain`
[error] one error found
[error] (Compile / compileIncremental) Compilation failed
[error] Total time: 0 s, completed Oct 4, 2023, 9:27:48 PM
[info] 10. Monitoring source files for root/run...
[info] Press <enter> to interrupt or '?' for more options.
関数の中でclass叩けるのはかなり新鮮
公式によると valで宣言されているメソッドはreassignmentできないらしい
にしてもエラーログ、なんか Rustっぽいなあ

同じメソッドでもscalaのもの?Javaのもの?色々あるのでこういう時はこれ使ったほうがいいとかベストプラクティスあったりしないのかな

こんな感じでいっぱいあるよね

codingにもおすすめのページらしい