Scala Advent Calendar 2020 - Qiita 3æ¥ç®ã§ã
ãã2,3å¹´ã®éã«Scalaã®éçºãã¼ã«ãã§ã¼ã³ã¯é²åãç¶ãã¦ãã¦ãScalaã®éçºä½é¨ã¯ãã¡ããã¡ãè¯ããªã£ã¦ãã¦ãã¾ããä¾ãã°ãæ°å¹´åã ã¨
- IDEã¯åºæ¬çã«IntelliJä¸æãvimã¨ãemacsã§éçºç°å¢ä½ããªãããªããã©ããªã大å¤
- IntelliJã®åãã§ãã¯ããã¾ãè¡ããªãã¦ãã¡ãã£ã¨è¤éãªã³ã¼ãæ¸ãã¨ã³ã³ãã¤ã«ã¯éãã®ã«ç»é¢ãçã£èµ¤ã«ãªã
- ã³ã³ãã¤ã«ãé ã(ã¤ã³ã¯ãªã¡ã³ã¿ã«ãã«ããã¦ãé ã)
ã¨ããæãã ã£ãã®ã§ãããç¾ç¶ã¯ããªãæ¹åããã¦ãã¦
- IntelliJ 以å¤ã«ã metals ã¨ãã Language Server ãããªã使ãããããªã£ã¦ãã¦ããããã¨ãã£ã¿ã§ç°¡åã«Scalaãæ¸ããããã«ãªã£ã
- IntelliJ ã metals ã³ã³ãã¤ã©(æ£ç¢ºã«ã¯ãã«ããµã¼ãã¼)ããç´æ¥ã³ã³ãã¤ã«ã¨ã©ã¼ãåãåãã®ã§ã³ã³ãã¤ã«éãã®ã«çã£èµ¤ã¨ãã¯ãªã
- Scalaã³ã³ãã¤ã©èªä½ã¯å°ããã¤æ©ããªã£ã¦ãã
- sbtã®incremental compilerã®æ¹åãåçã§ãsbtãç«ã¡ä¸ãã£ã±ãªãã«ãã¦ããå ´åã¯ããªãéçºå¹çãè¯ã
- ãã«ãã«ãã¯ãã£ã±ãæéããã
åºæ¬çã« OSX / VSCode ã§ã®ç°å¢æ§ç¯æ¹æ³ãæ¸ãã¦ããã¾ããInttelliJ ã®æ§ç¯ããã®ãã¡æ¸ããã
ã¾ã 2020å¹´ã ãã©ãããã2021å¹´ã ã2021å¹´ã£ã¦ã¿ã¤ãã«ã«ãã¡ãã£ãã
- Java ã®ã¤ã³ã¹ãã¼ã«
- sbt ã®ã¤ã³ã¹ãã¼ã«
- æ°ããsbtã®ããã¸ã§ã¯ããä½ã
- sbt ãç«ã¡ä¸ãã¦ã¿ã
- REPL ãç«ã¡ä¸ãã¦ã¿ã
- vscode ã®ã»ããã¢ãã
- ã³ã¼ããã©ã¼ããã
- Linter
- Worksheet æ©è½ã使ã£ã¦ã¿ãã
Java ã®ã¤ã³ã¹ãã¼ã«
ã¨ããããJava11ãå ¥ãã¦ããã°è¯ãã¨æãã¾ããJavaãã£ã¹ããªãã¥ã¼ã·ã§ã³ã¯ãããããããã©ãã¼ã«ã«ãªãä½ã§ãè¯ãããããªããã§ããã? oracle ã¨ã adoptopenjdk ã¨ãã§
brew
$ brew cask install java11
# ~/.zshrc ãªããã« export JAVA_HOME=`/usr/libexec/java_home -v 11`
$ java -version
ãã¦ãªããåºã¦ãã°OK
Homebrewã§ã¤ã³ã¹ãã¼ã«ã§ããJDKã¾ã¨ãï¼2019å¹´11ææç¹ï¼ - Qiita
java ã®ãã¼ã¸ã§ã³åãæ¿ã
- ã¢ããªã±ã¼ã·ã§ã³éçºããåã«ã¯javaã®ãã¼ã¸ã§ã³åãæ¿ããããã¨ã¯ãã¾ããªãã¯ã
- åã¯ç¨ã«ãã¼ã«ã«éçºç°å¢ã§ç°ãªãjavaã使ã£ã¦åä½ç¢ºèªãããããã¨ãããã®ã§ãjavaã®ãã¼ã¸ã§ã³ãã·ã¥ã·ã¥ã£ã¨åãæ¿ããããã« shyiko/jabba: (cross-platform) Java Version Manager ã使ã£ã¦ãã¾ãã使ãæ¹ã¯READMEèªãã°åããã¯ã
- ä»ã«ã¯ jenv/jenv: Manage your Java environment ã¨ã
sbt ã®ã¤ã³ã¹ãã¼ã«
scala ã®ä¸»æµãªãã«ããã¼ã«ã§ãã sbt - The interactive build tool ããã¦ã³ãã¼ããã¾ã
$ brew install sbt
- ããã§ãã¦ã³ãã¼ãããsbtã®ãã¼ã¸ã§ã³ã¯ãããªã«æ°ã«ããªãã¦è¯ãã§ã
- ããã§ãã¦ã³ãã¼ãããsbtã¯launcherãããã¸ã§ã¯ãæ¯ã®è¨å®ã§å®éã«å©ç¨ããsbtã®ãã¼ã¸ã§ã³ãæå®ã§ãã
æ°ããsbtã®ããã¸ã§ã¯ããä½ã
$ sbt new scala/hello-world.g8 [info] welcome to sbt 1.4.4 (AdoptOpenJDK Java 11.0.9) [info] set current project to tanishiking (in build file:/Users/tanishiking/src/github.com/tanishiking/) [info] set current project to tanishiking (in build file:/Users/tanishiking/src/github.com/tanishiking/) A template to demonstrate a minimal Scala application name [Hello World template]: hello Template applied in /Users/tanishiking/src/github.com/tanishiking/./hello
hello
ãã£ã¬ã¯ããªä»¥ä¸ã«ä»¥ä¸ã®ãããªããã¸ã§ã¯ããåºæ¥ã¾ãã
$ tree . |-- build.sbt |-- project | `-- build.properties `-- src `-- main `-- scala `-- Main.scala
build.sbt
- sbt ã®ãã«ãè¨å® sbt Reference Manual â å§ãã sbt
project/build.properties
sbt.version=1.4.3
ã¿ãããªã®ãæ¸ããã¦ãã¯ããããã§å©ç¨ããsbtã®ãã¼ã¸ã§ã³ãæå®ããã- ã«ã¼ããã£ã¬ã¯ããªã§
sbt
ãèµ·åããã¨ããã«æ¸ããã¦ããã¼ã¸ã§ã³ã®sbtããã¦ã³ãã¼ãããã
sbt ãç«ã¡ä¸ãã¦ã¿ã
build.sbt
ã®ãããã£ã¬ã¯ããªã«ç§»åãã¦sbt
ãèµ·åãã
$ sbt [info] [launcher] getting org.scala-sbt sbt 1.4.3 (this may take some time)... ... ... [info] welcome to sbt 1.4.3 (AdoptOpenJDK Java 11.0.9) [info] loading project definition from /Users/tanishiking/src/github.com/tanishiking/hello/project [info] loading settings for project hello from build.sbt ... [info] set current project to hello-world (in build file:/Users/tanishiking/src/github.com/tanishiking/hello/) [info] sbt server started at local:///Users/tanishiking/.sbt/1.0/server/baf7ac7b489b491bdcaa/sock [info] started sbt server sbt:hello-world> sbt:hello-world> compile [info] compiling 1 Scala source to /Users/tanishiking/src/github.com/tanishiking/hello/target/scala-2.13/classes ... update 0s [success] Total time: 3 s, completed Dec 2, 2020, 9:42:12 PM sbt:hello-world> run [info] running Main Hello, World! [success] Total time: 0 s, completed Dec 2, 2020, 9:42:18 PM
- sbt ã¯
$ sbt compile
ã®ããã« shell ããå®è¡ãããã¨ãåºæ¥ã¾ãããéçºä¸ã¯ sbt ã¯ç«ã¡ä¸ãã£ã±ãªãã«ãã¦ãããã¨ãæ¨å¥¨ããã¦ãã¾ã - ãªããªã
sbt
ãèµ·åãããã³ã«JVMãèµ·åãããã¨ã«ãªãã®ã§æéãããsbt
ã¯èµ·åãã¦ããéãåãåãScalaã³ã³ãã¤ã©ãå©ç¨ãããã®ã§ãcompileãç¹°ãè¿ããã¨ã§Scalaã³ã³ãã¤ã©ã®JITã温ã¾ã£ã¦ãã£ã¦é«éã«ã³ã³ãã¤ã«ãåãããã«ãªã£ã¦ãã
REPL ãç«ã¡ä¸ãã¦ã¿ã
sbt ãèµ·åãã¦ããããã console
ã¨æã£ã¦ã¿ã¾ããã
sbt:hello-world> console [info] Starting scala interpreter... Welcome to Scala 2.13.3 (OpenJDK 64-Bit Server VM, Java 11.0.9). Type in expressions for evaluation. Or try :help. scala>
sbt console ãããªã sbt ã§ãã¦ã³ãã¼ãããä¾åã©ã¤ãã©ãªã import åºæ¥ããããã®ã§ä¾¿å©
vscode ã®ã»ããã¢ãã
ã¨ããæãã«ãªã£ã¦ãã¾ããmetals 㯠scalameta ã¨ãã Scala ã®è§£æãã¼ã«ã»ããããã¼ã¹ã«ããLanguage Server Protocol å®è£ 㧠IntelliJ Scala ã«æ¬¡ãã·ã§ã¢ãèªã (https://scalacenter.github.io/scala-developer-survey-2019/) éçºãã¼ã«ã§ãã
å ç¨ã®ãã£ã¬ã¯ããªã§VSCode ãç«ã¡ä¸ã
$ code .
ãã¦ã³ãã¼ããå®äºããã build.sbt
ã *.scala
ãã¡ã¤ã«ãéãã¦ã¿ã¾ãããã以ä¸ã®ãããªãããã¢ãããåºã¦ããã®ã§ Import build
ãæ¼ãã¦æ«ããå¾
ã¡ä¸ããã
ããã§ã»ããã¢ããå®äºã§ã
ééã㦠Import build
ããããã人㯠VSCode ã®ã³ãã³ããã¬ãããéã㦠> metals: Import build
ãå®è¡ãã¦ãã
pro tip
ãªããIDEãcomopletionã¨ãè¿ãã¦ãããªããªã£ãã¨ãã¯ä¸åº¦ .metals
㨠.bloop
ã¨ãããã£ã¬ã¯ããªãåé¤ãã¦VSCodeãåèµ·åãã¾ãããã(IDEã®ãµã¼ãã¼å®å®ããããã¨æã£ã¦ãã)
ã³ã¼ããã©ã¼ããã
scalameta.org ã使ãã¾ããã
- CLI / sbt plugin / IDE ãã使ããããã«ãªã£ã¦ããã©åºæ¬çã«ã¯ sbt plugin ã IDE ãã使ãã®ãå人çã«ãããã
- ããã¸ã§ã¯ãã«ã¼ãã«
.scalafmt.conf
ãä½ã£ã¦version=2.7.4
ã¨ãæ¸ãã¦ãã ãã- ããã®ãã¼ã¸ã§ã³ã¯scalafmtã®ãã¼ã¸ã§ã³ Releases · scalameta/scalafmt · GitHub ãæ¸ãã¾ãã
VSCode ããããå ´å
- VSCode ã®ã³ãã³ããã¬ãããéãã¦
> Format Document
ãå®è¡ãã¦ã¿ã¾ããããéãã¦ãããã¡ã¤ã«ã«å¯¾ãã¦ãã©ã¼ããããå®è¡ããã¾ãã .scalafmt.conf
ãåå¨ããªãå ´åã¯ä»¥ä¸ã®ãããªãããã¢ãããåºã¦ããã®ã§Create '.scalafmt.conf'
ãæ¼ãã¦åæã«ãã¡ã¤ã«ãä½ã£ã¦ããã£ãããã¾ãããã- VSCode ã®
Format On Save
ãæå¹ã«ãã㨠save æã«åæã«ãã©ã¼ããããã¦ãããããã«ãªã
sbt ãã使ãå ´å
- scalameta/sbt-scalafmt: sbt plugin for Scalafmt ã使ãã¾ããã
project/plugins.sbt
ã«ä»¥ä¸ã®ããã«æ¸ã㦠sbt ãèµ·åãã¾ã- ç´ããããã®ã§ããã
sbt-scalafmt
ã®ãã¼ã¸ã§ã³ã¨ãä¸ã«æ¸ãã scalafmt ã®ãã¼ã¸ã§ã³ã¯å®å ¨ã«ç¬ç«ãã¦ãã¾ããsbt-scalafmt 㯠scalafmt æ¬ä½ããã¦ã³ãã¼ããã¦ãã¦ãããã使ã£ã¦ãã©ã¼ããããå®è¡ãããã¤
- ç´ããããã®ã§ããã
addSbtPlugin("org.scalameta" % "sbt-scalafmt" % "2.4.2")
sbt ãç«ã¡ä¸ã以ä¸ã®ã³ãã³ããæã¤ãã¨ã§å ¨ã¦ã®ãã¡ã¤ã«ããã©ã¼ããããã
sbt:hello-world> scalafmtAll
詳ããã¯
https://scalameta.org/scalafmt/docs/installation.html#task-keys
Linter
Scala ã£ã¦ããã¡ã¯ããªæãã® Linter ããªããã§ããã
ä¸å¿ã«ã¹ã¿ãã¤ãºããããã¦å°æ¥çã« Scala3 ã¨ã®äºææ§ãä¿ãããã㪠Linter(Refactoring tool) ã¨ãã¦ã¯ scalafix ãææãä»ã«ã¯ WartRemover ã¨ãããªãã ãã©ããããªã«ä½¿ããã¦ãå°è±¡ãªã
Worksheet æ©è½ã使ã£ã¦ã¿ãã
- worksheet æ©è½ã¯é«ç´REPLã¿ãããªæ©è½? Scala ã®æ¨æºã©ã¤ãã©ãªã®æåã確èªãããããã®ã«ä¾¿å©
- goto definition ãªã©ã® IDE ã®æ©è½ã®ã¤ãã REPL ã¿ããã«èããã¨è¯ããã
- 詳ãã㯠https://scalameta.org/metals/docs/editors/vscode.html#worksheets
ã³ãã³ããã¬ãããã New Scala File
ãé¸ã³ãWorksheets
ãé¸ã¶ã
xxx.worksheet.sc
ã¨ãããã¡ã¤ã«ãã§ããã®ã§ãããã«é©å½ãª scala ã¹ã¯ãªãããæ¸ãã¦ã¿ããã以ä¸ã®ãããªæãã§ã³ã¡ã³ãã«è©ä¾¡çµæã表示ãããã