MacBookにScalaを入れて、REPL環境・スクリプト実行・コンパイル実行で動作テスト

MacBookにJava（AdoptOpenJDK）とScalaをインストールする手順です。

###参考にさせていただいたウェブページ

https://qiita.com/sudachi0114/items/ea0b5eef5ffd3e37fdd3


- scalaはまだインストールされていない状態

```bash:Terminal
diy@electronnoMacBook-Pro ~ % scala
zsh: command not found: scala
diy@electronnoMacBook-Pro ~ %
```

- brew installできるscalaの各バージョン番号を確認

```bash:Terminal
diy@electronnoMacBook-Pro ~ % brew search scala
==> Formulae
scala            scala@2.11       scala@2.12       scalaenv         scalapack        scalariform      scalastyle       carla
==> Casks
scala-ide
diy@electronnoMacBook-Pro ~ %
```

- brew installできるsbtの各バージョン番号を確認

```bash:Terminal
diy@electronnoMacBook-Pro ~ %  brew search sbt
==> Formulae
sbt                                sbt@0.13                           sbtenv                             srt ✔
diy@electronnoMacBook-Pro ~ %
```

###まずは、Javaをインストール（AdoptOpenJDK）

- brewリポジトリにAdoptOpenJDKを追加登録

```bash:Terminal
diy@electronnoMacBook-Pro ~ % brew tap AdoptOpenJDK/openjdk
Updating Homebrew...
==> Auto-updated Homebrew!
Updated 2 taps (homebrew/core and homebrew/cask).
==> New Formulae
corepack               pdf2djvu               sigrok-cli             twty                   west                   

（ 省略 ）

==> Tapping adoptopenjdk/openjdk
Cloning into '/usr/local/Homebrew/Library/Taps/adoptopenjdk/homebrew-openjdk'...
remote: Enumerating objects: 1996, done.
remote: Counting objects: 100% (60/60), done.
remote: Compressing objects: 100% (22/22), done.
remote: Total 1996 (delta 44), reused 49 (delta 38), pack-reused 1936
Receiving objects: 100% (1996/1996), 372.27 KiB | 2.51 MiB/s, done.
Resolving deltas: 100% (1424/1424), done.
Tapped 47 casks (69 files, 521.9KB).
diy@electronnoMacBook-Pro ~ %
```

- brew installできるopenjdkの各バージョン番号を確認

```bash:Terminal
diy@electronnoMacBook-Pro ~ % brew search openjdk
==> Formulae
openjdk                     openjdk@11                  openjdk@8                   openj9                      openvdb
==> Casks
adoptopenjdk ✔                     adoptopenjdk12                     adoptopenjdk14-jre                 adoptopenjdk16-openj9
adoptopenjdk-jre                   adoptopenjdk12-jre                 adoptopenjdk14-openj9              adoptopenjdk16-openj9-jre
adoptopenjdk-openj9                adoptopenjdk12-openj9              adoptopenjdk14-openj9-jre          adoptopenjdk8
adoptopenjdk-openj9-jre            adoptopenjdk12-openj9-jre          adoptopenjdk14-openj9-jre-large    adoptopenjdk8-jre
adoptopenjdk-openj9-jre-large      adoptopenjdk12-openj9-jre-large    adoptopenjdk14-openj9-large        adoptopenjdk8-openj9
adoptopenjdk-openj9-large          adoptopenjdk12-openj9-large        adoptopenjdk15                     adoptopenjdk8-openj9-jre
adoptopenjdk10                     adoptopenjdk13                     adoptopenjdk15-jre                 adoptopenjdk8-openj9-jre-large
adoptopenjdk11                     adoptopenjdk13-jre                 adoptopenjdk15-openj9              adoptopenjdk8-openj9-large
adoptopenjdk11-jre                 adoptopenjdk13-openj9              adoptopenjdk15-openj9-jre          adoptopenjdk9
adoptopenjdk11-openj9              adoptopenjdk13-openj9-jre          adoptopenjdk15-openj9-jre-large    microsoft-openjdk
adoptopenjdk11-openj9-jre          adoptopenjdk13-openj9-jre-large    adoptopenjdk15-openj9-large
adoptopenjdk11-openj9-jre-large    adoptopenjdk13-openj9-large        adoptopenjdk16
adoptopenjdk11-openj9-large        adoptopenjdk14                     adoptopenjdk16-jre
diy@electronnoMacBook-Pro ~ % 
```

- brew cask installでopenjdkを入れる
- まず、caskを入れる。

```bash:Terminal
diy@electronnoMacBook-Pro ~ % brew install cask
==> Downloading https://ghcr.io/v2/homebrew/core/coreutils/manifests/9.0
######################################################################## 100.0%

（ 省略 ）

==> Installing dependencies for cask: coreutils and emacs
==> Installing cask dependency: coreutils

（ 省略 ）

==> Caveats
==> cask
Emacs Lisp files have been installed to:
  /usr/local/share/emacs/site-lisp/cask
diy@electronnoMacBook-Pro ~ % 
```

- brew caskは廃止されている。caskはオプションになっていた。
- OpenJDKのバージョンのうち、Scalaのインストール手順を開設した新しいウェブページでよく選択されているv.11を選びました。

https://www.code-smith.dev/entry/2021/01/02/082749

https://qiita.com/282Haniwa/items/c29da661a929010c5e49

> こちらのリンクからOpenJDK 11 (LTS)とHotSpotを選択してダウンロードしましょう。

https://qiita.com/sudachi0114/items/ea0b5eef5ffd3e37fdd3

> $ brew cask install adoptopenjdk11

```bash:Terminal
diy@electronnoMacBook-Pro ~ % brew cask install adoptopenjdk11
Error: Unknown command: cask

diy@electronnoMacBook-Pro ~ %
```

```bash:Terminal
diy@electronnoMacBook-Pro ~ % brew install adoptopenjdk11 --cask
Updating Homebrew...
==> Auto-updated Homebrew!
Updated 1 tap (homebrew/core).
No changes to formulae.

==> Downloading https://github.com/AdoptOpenJDK/openjdk11-binaries/releases/download/jdk-11.0.11%2B9/OpenJDK11U-jdk_x64_mac_hotspot_11.0.11_9

（ 省略 ）

installer: Package name is AdoptOpenJDK
installer: Installing at base path /
installer: The install was successful.
package-id: net.adoptopenjdk.11.jdk
version: 11.0.11+9
volume: /
location: 
install-time: 1636108944
🍺  adoptopenjdk11 was successfully installed!
diy@electronnoMacBook-Pro ~ % 
```

- 環境変数java_homeをパスに登録

```bash:Terminal
diy@electronnoMacBook-Pro ~ % /usr/libexec/java_home -v 11
/Library/Java/JavaVirtualMachines/adoptopenjdk-11.jdk/Contents/Home
diy@electronnoMacBook-Pro ~ % 
```

```bash:Terminal
diy@electronnoMacBook-Pro ~ % ls /usr/libexec/java_home      
/usr/libexec/java_home
diy@electronnoMacBook-Pro ~ %
```

```bash:Terminal
diy@electronnoMacBook-Pro ~ % export JAVA_HOME=/Library/Java/JavaVirtualMachines/adoptopenjdk-11.jdk/Contents/Home
diy@electronnoMacBook-Pro ~ % echo $JAVA_HOME
/Library/Java/JavaVirtualMachines/adoptopenjdk-11.jdk/Contents/Home
diy@electronnoMacBook-Pro ~ % 
```

```bash:Terminal
diy@electronnoMacBook-Pro ~ % PATH=${JAVA_HOME}/bin:${PATH}
diy@electronnoMacBook-Pro ~ % echo $PATH

（ 省略 ）

diy@electronnoMacBook-Pro ~ % 
```


```bash:Terminal
diy@electronnoMacBook-Pro ~ % source ~/.bashrc
diy@electronnoMacBook-Pro ~ %
```

- Terminalで、javaコマンドが認識されるようになった

```bash:Terminal
diy@electronnoMacBook-Pro ~ % java -version
openjdk version "11.0.11" 2021-04-20
OpenJDK Runtime Environment AdoptOpenJDK-11.0.11+9 (build 11.0.11+9)
OpenJDK 64-Bit Server VM AdoptOpenJDK-11.0.11+9 (build 11.0.11+9, mixed mode)
diy@electronnoMacBook-Pro ~ % 
```

- Terminalで、javacコマンドも認識される

```bash:Terminal
diy@electronnoMacBook-Pro ~ % javac -version
javac 11.0.11
diy@electronnoMacBook-Pro ~ % 
```

- brew installでscalaを入れる

```bash:Terminal
diy@electronnoMacBook-Pro ~ % brew install scala
==> Downloading https://ghcr.io/v2/homebrew/core/openjdk/manifests/17.0.1
######################################################################## 100.0%

（ 省略 ）

==> Installing dependencies for scala: openjdk
==> Installing scala dependency: openjdk
==> Pouring openjdk--17.0.1.catalina.bottle.tar.gz
🍺  /usr/local/Cellar/openjdk/17.0.1: 639 files, 305.3MB
==> Installing scala
==> Pouring scala--2.13.6.all.bottle.tar.gz
==> Caveats
To use with IntelliJ, set the Scala home to:
  /usr/local/opt/scala/idea
==> Summary
🍺  /usr/local/Cellar/scala/2.13.6: 42 files, 23.8MB
==> Caveats
==> scala
To use with IntelliJ, set the Scala home to:
  /usr/local/opt/scala/idea
diy@electronnoMacBook-Pro ~ % 
```

- Terminalで、scalaコマンドが認識されるようになった

```bash:Terminal
diy@electronnoMacBook-Pro ~ % scala -version
Scala code runner version 2.13.6 -- Copyright 2002-2021, LAMP/EPFL and Lightbend, Inc.
diy@electronnoMacBook-Pro ~ % 
```

- Scalaの対話インタプリタREPLをscalaコマンドで起動できる

```bash:Terminal
diy@electronnoMacBook-Pro ~ % scala
Welcome to Scala 2.13.6 (OpenJDK 64-Bit Server VM, Java 11.0.11).
Type in expressions for evaluation. Or try :help.

scala> 

scala> println("Hello World")
Hello World

scala> sys.exit(1)
diy@electronnoMacBook-Pro ~ %
```

- __*:q*__でRPPLを抜けることもできる

```bash:Terminal
diy@electronnoMacBook-Pro ~ % scala
Welcome to Scala 2.13.6 (OpenJDK 64-Bit Server VM, Java 11.0.11).
Type in expressions for evaluation. Or try :help.
 
scala> :q
diy@electronnoMacBook-Pro ~ %
```

- brew installでsbtを入れる

```bash:Terminal
diy@electronnoMacBook-Pro ~ % brew install sbt
Updating Homebrew...
==> Auto-updated Homebrew!
Updated 2 taps (homebrew/core and homebrew/cask).
==> Updated Formulae
Updated 1 formula.
==> Updated Casks
Updated 1 cask.

==> Downloading https://ghcr.io/v2/homebrew/core/sbt/manifests/1.5.5-1
######################################################################## 100.0%
==> Downloading https://ghcr.io/v2/homebrew/core/sbt/blobs/

（ 省略 ）

==> Pouring sbt--1.5.5.catalina.bottle.1.tar.gz
==> Caveats
You can use $SBT_OPTS to pass additional JVM options to sbt.
Project specific options should be placed in .sbtopts in the root of your project.
Global settings should be placed in /usr/local/etc/sbtopts

Homebrew's installation does not include `sbtn`.
==> Summary
🍺  /usr/local/Cellar/sbt/1.5.5: 8 files, 3.7MB
diy@electronnoMacBook-Pro ~ % 
```

- sbt consoleと打つと、ScalaのREPLが立ち上がる

```bash:Terminal
diy@electronnoMacBook-Pro ~ % sbt console
copying runtime jar...
[info] [launcher] getting org.scala-sbt sbt 1.5.5  (this may take some time)...
[info] [launcher] getting Scala 2.12.14 (for sbt)...
[info] Updated file /Users/diy/project/build.properties: set sbt.version to 1.5.5
[info] welcome to sbt 1.5.5 (AdoptOpenJDK Java 11.0.11)
[info] loading project definition from /Users/diy/project
[info] Updating 
https://repo1.maven.org/maven2/jline/jline/2.14.6/jline-2.14.6.pom
  100.0% [##########] 19.4 KiB (114.9 KiB / s)
https://repo1.maven.org/maven2/org/fusesource/jansi/jansi/1.12/jansi-1.12.pom
  100.0% [##########] 3.6 KiB (8.9 KiB / s)
https://repo1.maven.org/maven2/org/fusesource/jansi/jansi-project/1.12/jansi-project-1.12.pom
  100.0% [##########] 11.5 KiB (65.8 KiB / s)
https://repo1.maven.org/maven2/org/fusesource/fusesource-pom/1.11/fusesource-pom-1.11.pom
  100.0% [##########] 14.2 KiB (99.5 KiB / s)
[info] Resolved  dependencies
[info] Fetching artifacts of 
https://repo1.maven.org/maven2/org/fusesource/jansi/jansi/1.12/jansi-1.12.jar
  100.0% [##########] 148.2 KiB (1.2 MiB / s)
[info] Fetched artifacts of 
[info] set current project to diy (in build file:/Users/diy/)
[info] Non-compiled module 'compiler-bridge_2.12' for Scala 2.12.14. Compiling...
[info]   Compilation completed in 8.779s.
[info] Starting scala interpreter...
Welcome to Scala 2.12.14 (OpenJDK 64-Bit Server VM, Java 11.0.11).
Type in expressions for evaluation. Or try :help.

scala> :q

[success] Total time: 18 s, completed 2021/11/05 19:55:00
diy@electronnoMacBook-Pro ~ % 
```

- sbt ver. 1.5.5が入った

```bash:Terminal
diy@electronnoMacBook-Pro ~ % sbt --version
sbt version in this project: 1.5.5
sbt script version: 1.5.5
diy@electronnoMacBook-Pro ~ % 
```

- sbtの動作確認

- 作業ディレクトリを作り、カレント・ディレクトリにする

```bash:Terminal
diy@electronnoMacBook-Pro ~ % mkdir myfolder
diy@electronnoMacBook-Pro ~ % cd myfolder
```

- Scalaのオブジェクトを定義したファイルを作成する

```bash:Terminal
diy@electronnoMacBook-Pro myfolder % vim HelloWorld.scala
diy@electronnoMacBook-Pro myfolder % cat HelloWorld.scala
object HelloWorld {
  def main(args: Array[String]): Unit = {
    println("Hello, Scala World!")
  }
}
diy@electronnoMacBook-Pro myfolder %
```

- build.sbtファイルを作成する。

```bash:Terminal
diy@electronnoMacBook-Pro myfolder % vim build.sbt
diy@electronnoMacBook-Pro myfolder % cat build.sbt
scalaVersion := "2.12.10"

scalacOptions ++= Seq("-deprecation", "-feature", "-unchecked", "-Xlint")
diy@electronnoMacBook-Pro myfolder % 
```

- sbtコマンドを実行する
- 実行に成功した（以下が表示された）

>```bash:Terminal
>[info] running HelloWorld 
Hello, Scala World!
[success] Total time: 12 s, completed 2021/11/05 19:56:42
sbt:myfolder> 
>```

```bash:Terminal
diy@electronnoMacBook-Pro myfolder % sbt
[info] Updated file /Users/diy/myfolder/project/build.properties: set sbt.version to 1.5.5
[info] welcome to sbt 1.5.5 (AdoptOpenJDK Java 11.0.11)
[info] loading project definition from /Users/diy/myfolder/project
[info] loading settings for project myfolder from build.sbt ...
[info] set current project to myfolder (in build file:/Users/diy/myfolder/)
[info] sbt server started at local:///Users/diy/.sbt/1.0/server/ef9509b4b1cee59e2b32/sock
[info] started sbt server
sbt:myfolder> 
sbt:myfolder> run
[info] Updating 
[info] Resolved  dependencies
[info] Updating 
https://repo1.maven.org/maven2/org/scala-lang/scala-compiler/2.12.10/scala-compiler-2.12.10.pom
  100.0% [##########] 2.4 KiB (15.3 KiB / s)
https://repo1.maven.org/maven2/org/scala-lang/scala-reflect/2.12.10/scala-reflect-2.12.10.pom
  100.0% [##########] 1.8 KiB (10.7 KiB / s)
[info] Resolved  dependencies
[info] Fetching artifacts of 
https://repo1.maven.org/maven2/org/scala-lang/scala-reflect/2.12.10/scala-reflect-2.12.10.jar
  100.0% [##########] 3.5 MiB (2.7 MiB / s)
https://repo1.maven.org/maven2/org/scala-lang/scala-library/2.12.10/scala-library-2.12.10.jar
  100.0% [##########] 5.0 MiB (3.4 MiB / s)
https://repo1.maven.org/maven2/org/scala-lang/scala-compiler/2.12.10/scala-compiler-2.12.10.jar
  100.0% [##########] 10.2 MiB (6.7 MiB / s)
[info] Fetched artifacts of 
[info] compiling 1 Scala source to /Users/diy/myfolder/target/scala-2.12/classes ...
[info] Non-compiled module 'compiler-bridge_2.12' for Scala 2.12.10. Compiling...
[info]   Compilation completed in 8.546s.
[info] running HelloWorld 
Hello, Scala World!
[success] Total time: 12 s, completed 2021/11/05 19:56:42
sbt:myfolder> 
```

```bash:Terminal
sbt:myfolder> exit
[info] shutting down sbt server
diy@electronnoMacBook-Pro myfolder %
```

- Terminalを抜ける

```bash:Terminal
 cd ..
diy@electronnoMacBook-Pro ~ % exit

[プロセスが完了しました]
```

###Terminalを起動し直して、Scalaコマンドが認識されるか確認

- javaコマンドが認識される

```bash:Terminal
diy@electronnoMacBook-Pro ~ % java --version
openjdk 16.0.1 2021-04-20
OpenJDK Runtime Environment AdoptOpenJDK-16.0.1+9 (build 16.0.1+9)
OpenJDK 64-Bit Server VM AdoptOpenJDK-16.0.1+9 (build 16.0.1+9, mixed mode, sharing)
diy@electronnoMacBook-Pro ~ % 
```

- sbtコマンドも認識される

```bash:Terminal
diy@electronnoMacBook-Pro ~ % sbt --version
copying runtime jar...
WARNING: A terminally deprecated method in java.lang.System has been called
WARNING: System::setSecurityManager has been called by sbt.TrapExit$ (file:/Users/diy/.sbt/boot/scala-2.12.14/org.scala-sbt/sbt/1.5.5/run_2.12-1.5.5.jar)
WARNING: Please consider reporting this to the maintainers of sbt.TrapExit$
WARNING: System::setSecurityManager will be removed in a future release
sbt version in this project: 1.5.5
sbt script version: 1.5.5
diy@electronnoMacBook-Pro ~ %
```

- scalaコマンドも認識される

```bash:Terminal
diy@electronnoMacBook-Pro ~ % scala
Welcome to Scala 2.13.6 (OpenJDK 64-Bit Server VM, Java 17.0.1).
Type in expressions for evaluation. Or try :help.

scala> 1 + 12
val res0: Int = 13

scala> println("xyz")
xyz

scala> :q
diy@electronnoMacBook-Pro ~ %     
```

###コンパイラ__*scalac*__の動作確認

浅海（著）『ボクらのScala 次世代ェクト・トラッキング徹底入門』（SoftBank Creative）所収のコードを写経して実行しました。

```bash:Terminal
diy@electronnoMacBook-Pro myfolder% vim TextCounter160.scala
```

```Scala:TextCounter160.scala 
package com.example

import scala.io.Source

object TextCounter06 {
  def main(args: Array[String]) {
    val counter = new Counter06()
    val filenames: Array[String] = args
    for (filename <- filenames) {
        counter.counterText(filename)
    }
    val charCount = counter.charCount
    val wordCount = counter.wordCount
    val lineCount = counter.lineCount
    println("文字数 = %s, 単語数 = %s, 行数 = %s".format(charCount, wordCount, lineCount))
  }
}

class Counter06 {
  var charCount = 0
  var wordCount = 0
  var lineCount = 0

  def counterText(filename:String) {
    val src = Source.fromFile(filename)
    val lines = src.getLines
    for (line <- lines) {
      charCount += line.length
      val words = line.split(" ")
      wordCount += words.length
      lineCount += 1
    }
  }
}
```

- テキストファイルを用意（１つ目）

（ソース）
https://www.nytimes.com/

```text:sample.txt
Infighting by Democrats Delays Infrastructure and Safety Net Bills
The House is pushing to vote on the Senate’s bipartisan infrastructure bill but is putting on hold an earlier plan to vote on Democrats’ social policy bill first.
The move comes after conservative-leaning Democrats insisted on a cost estimate for the social policy bill before committing to a vote.
```

- テキストファイルを用意（２つ目）
（ソース）
- https://www.bbc.com/news/uk-scotland-glasgow-west-59165781

```text:sample2.txt
Greta Thunberg has told a mass rally in Glasgow that the COP26 climate summit has been a "failure".

The Swedish activist had earlier joined thousands of young people - including striking school pupils - for a march through the city.

She addressed the crowd when it arrived in George Square, saying "immediate and drastic" cuts to emissions are needed.

The march was organised by Fridays for Future Scotland, a group founded by youngsters inspired by Ms Thunberg.

It was one of the largest of a series of demonstrations taking place throughout the summit, which is being held in the city.
```

- TextCounter160.scalaをコンパイル

```bash:Terminal
diy@electronnoMacBook-Pro myfolder % scalac TextCounter160.scala
warning: 2 deprecations (since 2.13.0)
warning: 1 deprecation (since 2.13.3)
warning: 3 deprecations in total; re-run with -deprecation for details
3 warnings
diy@electronnoMacBook-Pro myfolder % 
```

- パッケージ名「com.example」を修飾子に付けた__*scala com.example.TextCounter06*__を実行
- 引数として、テキストファイルのファイル名（相対パス）を１つだけ渡す
- 渡したファイル名は、__*sample.txt*__

```bash:Terminal
diy@electronnoMacBook-Pro myfolder % scala com.example.TextCounter06 sample.txt
文字数 = 363, 単語数 = 58, 行数 = 3
diy@electronnoMacBook-Pro myfolder %
```

- 引数として、テキストファイルのファイル名（相対パス）を１つだけ渡す
- 渡したファイル名は、__*sample2.txt*__

```bash:Terminal
diy@electronnoMacBook-Pro myfolder % scala com.example.TextCounter06 sample2.txt
文字数 = 583, 単語数 = 104, 行数 = 9
diy@electronnoMacBook-Pro myfolder % 
```

- 引数として、テキストファイルのファイル名（相対パス）を２つ渡す
- 渡したファイル名は、__*sample.txt*__と__*sample2.txt*__

```bash:Terminal
diy@electronnoMacBook-Pro myfolder % scala com.example.TextCounter06 sample.txt sample2.txt
文字数 = 946, 単語数 = 162, 行数 = 12
diy@electronnoMacBook-Pro myfolder %
```

- 引数として、テキストファイルのファイル名（相対パス）を３つ渡す
- 渡したファイル名は、__*sample.txt*__と、__*sample2.txt*２つ__

```bash:Terminal
diy@electronnoMacBook-Pro myfolder % scala com.example.TextCounter06 sample.txt sample2.txt sample2.txt
文字数 = 1529, 単語数 = 266, 行数 = 21
diy@electronnoMacBook-Pro myfolder % 
```

- 何度実行しても同じ結果が返る

```bash:Terminal
diy@electronnoMacBook-Pro myfolder % scala com.example.TextCounter06 sample.txt                        
文字数 = 363, 単語数 = 58, 行数 = 3
diy@electronnoMacBook-Pro myfolder % scala com.example.TextCounter06 sample.txt sample2.txt sample2.txt sample2.txt
文字数 = 2112, 単語数 = 370, 行数 = 30
diy@electronnoMacBook-Pro myfolder %
```


###スクリプトファイルをコンパイルせずに実行する

```Scala:hello.scala
println("Hello !")
```

```bash:Terminal
diy@electronnoMacBook-Pro scala_test % vim hello.scala
diy@electronnoMacBook-Pro scala_test % cat hello.scala
println("Hello !")
diy@electronnoMacBook-Pro scala_test 
```

```bash:Terminal
diy@electronnoMacBook-Pro scala_test % scala hello.scala 
Hello !
diy@electronnoMacBook-Pro scala_test %
```

###スクリプトファイルをREPL環境に読み込む（__*:load*コマンド__）

```bash:Terminal
diy@electronnoMacBook-Pro scala_test % scala
Welcome to Scala 2.13.6 (OpenJDK 64-Bit Server VM, Java 17.0.1).
Type in expressions for evaluation. Or try :help.

scala> :load hello.scala
val args: Array[String] = Array()
Loading hello.scala...
Hello !

scala> :q
diy@electronnoMacBook-Pro scala_test %
```


今度は、次のウェブページに掲載されているコードを写経して、クラス継承をやってみる

- https://r9.hateblo.jp/entry/2016/01/16/233058

```Scala:class_inheritance.scala
class Bar(val a: Int = 123, val b: Int = 234)

class Baz() extends Bar

val baz = new Baz()
baz.a //=> 123
baz.b //=> 234


class Foo() extends Bar(b = 345) // 名前付き引数で渡す

val foo = new Foo()

println(foo.a)
println(foo.b)
```

```bash:Terminal
diy@electronnoMacBook-Pro scala_test % scala class_inheritance.scala 
class_inheritance.scala:6: warning: a pure expression does nothing in statement position; multiline expressions may require enclosing parentheses
baz.a //=> 123
    ^
class_inheritance.scala:7: warning: a pure expression does nothing in statement position; multiline expressions may require enclosing parentheses
baz.b //=> 234
    ^
123
345
diy@electronnoMacBook-Pro scala_test %
```

- Bazクラスのインスタンスをnewキーワードで生成する操作と、インスタンス変数aとbの出力をREPL環境で実行する

```Scala:class_inheritance.scala
class Bar(val a: Int = 123, val b: Int = 234)

class Baz() extends Bar

val baz = new Baz()
baz.a //=> 123
baz.b //=> 234


class Foo() extends Bar(b = 345) // 名前付き引数で渡す

//val foo = new Foo()

//println(foo.a)
//println(foo.b)
```

```bash:Terminal
diy@electronnoMacBook-Pro scala_test % scala
Welcome to Scala 2.13.6 (OpenJDK 64-Bit Server VM, Java 17.0.1).
Type in expressions for evaluation. Or try :help.

scala> :load class_inheritance.scala
val args: Array[String] = Array()
Loading class_inheritance.scala...
class Bar
class Baz
val baz: Baz = Baz@629b780f
val res0: Int = 123
val res1: Int = 234
class Foo

scala> val foo = new Foo()
val foo: Foo = Foo@44a13699

scala> println(foo.a)
123

scala> println(foo.b)
345

scala> :q
diy@electronnoMacBook-Pro scala_test % 
```

- scalacコマンドでコンパイルして、生成された実行ファイルを実行する

```Scala:class_inheritance.scala   
object Test {
  def main(args: Array[String]){
     val baz = new Baz()
     println(baz.a)
     println(baz.b)

     val foo = new Foo() 
    
     println(foo.a)
     println(foo.b)
     }
  class Bar(val a: Int = 123, val b: Int = 234)
  class Baz() extends Bar
  class Foo() extends Bar(b = 345)
}
```

```bash:Terminal
diy@electronnoMacBook-Pro scala_test % scalac class_inheritance.scala
warning: 1 deprecation (since 2.13.0); re-run with -deprecation for details
1 warning
diy@electronnoMacBook-Pro scala_test % scala Test                    
123
234
123
345
diy@electronnoMacBook-Pro scala_test % 
```