Skip to content

Commit 9980090

Browse files
committed
step2の記述改善
1 parent 95ca2ee commit 9980090

File tree

8 files changed

+41
-25
lines changed

8 files changed

+41
-25
lines changed

docs/step02/01_Scala3で追加された地味に嬉しい系の機能追加.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
## 概要
2424

25-
Scala3 で新しく追加された機能の中から「今まで冗長だった記述が短くなる!地味に嬉しい!」という機能を集めてみました。本リポジトリの対応するサンプルコードと併せて見ていきましょう。
25+
Scala3 で新しく追加された機能の中から「今まで冗長だった記述が短くなる!地味に嬉しい!」という機能を集めてみました。本リポジトリの対応するサンプルコードと併せて見ていきましょう。
2626

2727

2828
## ドキュメント参照先
@@ -62,9 +62,9 @@ https://dotty.epfl.ch/docs/reference/changed-features/match-syntax.html
6262

6363
https://dotty.epfl.ch/docs/reference/other-new-features/trait-parameters.html
6464

65-
- class のパラメータと同じように trait にもパラメータを持たせられるようになった
66-
- trait への引数は、trait が初期化される直前に評価される
67-
- 同一 trait の extends など、曖昧な定義をするとコンパイルエラー
65+
- `class` のパラメータと同じように `trait` にもパラメータを持たせられるようになった
66+
- `trait` への引数は、`trait` が初期化される直前に評価される
67+
- 同一 `trait``extends` など、曖昧な定義をするとコンパイルエラー
6868

6969
## Universal Apply Methods
7070

@@ -113,7 +113,7 @@ https://dotty.epfl.ch/docs/reference/changed-features/operators.html
113113
### Syntax Change
114114

115115
- `infix` 演算子を複数行で書く場合に、行末ではなく行頭に書けるようになった(A leading infix operator)
116-
- この構文を動作させるために、ルールが変更され、A leading infix operator の前にセミコロンを推論しないようになった
116+
- この構文を動作させるために、ルールが変更され、行頭の `infix` 演算子の前のセミコロンを推論しないようになった
117117

118118

119119
## Extension Methods

step02/src/main/scala/com/github/shinharad/gettingStartedWithScala3/DroppedWeakConformance.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ val no1 = List(1.0, math.sqrt(3.0), 0, -3.3) // : List[Double]
88
// しかし、Scala2 では weak conformance の対象に Char も含まれていたため、
99
// 以下も List[Double] として型付けされていた
1010
// これは、意図した使い方ではなかった
11-
def no2 =
11+
def no2: List[Double] =
1212
val n: Int = 3
1313
val c: Char = 'X' // Char が Double に変換されていた
1414
val d: Double = math.sqrt(3.0)

step02/src/main/scala/com/github/shinharad/gettingStartedWithScala3/MatchExpressions.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ package matchExpressions
44
//---
55
// マッチ式を連鎖させることができる
66
// brasesは省略可能
7-
def no1 =
7+
def no1: Int =
88
val xs: List[Int] = ???
99

1010
xs match {
@@ -24,7 +24,7 @@ def no1 =
2424

2525
//---
2626
// ピリオドの後に書くことができる
27-
def no2 =
27+
def no2: String =
2828
val xs: List[Int] = ???
2929

3030
if xs.match
@@ -35,7 +35,7 @@ def no2 =
3535

3636
//---
3737
// `x: T match { ... }` はサポートされなくなったので、`(x: T) match { ... }` と書く
38-
def no3 =
38+
def no3: Int =
3939
val x: Int = ???
4040

4141
// NG

step02/src/main/scala/com/github/shinharad/gettingStartedWithScala3/ParameterUntupling.scala

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.github.shinharad.gettingStartedWithScala3
22
package parameterUntupling
33

4-
def parameterUntupling =
4+
def parameterUntupling(): Unit =
55
val xs: List[(Int, Int)] = ???
66

77
// 今までは
@@ -15,3 +15,5 @@ def parameterUntupling =
1515
}
1616

1717
xs map (_ + _)
18+
19+
()

step02/src/main/scala/com/github/shinharad/gettingStartedWithScala3/RulesForOperators.scala

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ trait MultiSet[T]:
1818
@targetName("intersection")
1919
def *(other: MultiSet[T]): MultiSet[T]
2020

21-
def no1 =
21+
def no1: Unit =
2222
val s1, s2: MultiSet[Int] = ???
2323

2424
s1 union s2 // OK
@@ -34,7 +34,7 @@ def no1 =
3434
s1.*(s2) // also OK, but unusual
3535

3636
// 型にも使用できる
37-
def no2 =
37+
def no2: Unit =
3838
infix type or[X, Y]
3939
val x: String or Int = ???
4040

@@ -52,14 +52,14 @@ trait Infix:
5252
//---
5353
// Syntax Change
5454

55-
// infix 演算子が、複数行式の行頭に現れるようになった
55+
// infix 演算子を複数行の行頭で書けるようになった
5656

57-
def no3 =
57+
def no3: Unit =
5858
val str = "hello"
5959
++ " world"
6060
++ "!"
6161

62-
def no4 =
62+
def no4: Unit =
6363
val x: Int = ???
6464
val xs: List[Int] = ???
6565

@@ -76,7 +76,7 @@ def no4 =
7676
xs.exists(_ > 0) ||
7777
xs.isEmpty
7878

79-
def no5() =
79+
def no5(): Int =
8080

8181
// Scala3 では ??? を infix 演算子として扱うのでこれはコンパイルエラー
8282
// println("hello")

step02/src/main/scala/com/github/shinharad/gettingStartedWithScala3/TraitParameters.scala

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
package com.github.shinharad.gettingStartedWithScala3
22
package traitParameters
33

4+
import scala.util.chaining.*
5+
46
trait Greeting(val name: String):
57
def msg = s"How are you, $name"
68

79
class C extends Greeting("Bob"):
810
println(msg)
911

12+
@main def no1(): Unit =
13+
new C
14+
1015
//---
1116
// 曖昧な定義はコンパイルエラー
1217

@@ -27,6 +32,10 @@ trait FormalGreeting extends Greeting:
2732
// パラメータを設定した Greeting を extends してあげるとコンパイルが通る
2833
class E extends Greeting("Bob"), FormalGreeting
2934

35+
@main def no2(): Unit =
36+
(new E).msg
37+
.tap(println)
38+
3039
// 色々応用できそう
3140

3241
//---
@@ -52,3 +61,8 @@ trait Greeting3(val namename: String):
5261
def msgmsg = s"How are you, $namename"
5362

5463
class H extends Greeting("Bob"), Greeting3("Bill")
64+
65+
@main def no3(): Unit =
66+
(new H)
67+
.tap(x => println(x.msgmsg))
68+
.tap(x => println(x.msg))

step02/src/main/scala/com/github/shinharad/gettingStartedWithScala3/UniversalApplyMethods.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ package universalApplyMethods
77
class StringBuilder(s: String):
88
def this() = this("")
99

10-
def no1() =
10+
def no1(): Unit =
1111
val sb1 = new StringBuilder("abc")
1212
// val sb2 = StringBuilder("abc") // compile error
1313
val sb2 = new StringBuilder()
@@ -23,7 +23,7 @@ class StringBuilder2(s: String):
2323
inline def apply(): StringBuilder2 = new StringBuilder2()
2424

2525
// その結果、new を書かなくてもインスタンスが生成できるようになる
26-
def no2() =
26+
def no2(): Unit =
2727
val sb1 = StringBuilder2("abc")
2828
val sb2 = StringBuilder2()
2929

@@ -42,6 +42,6 @@ object StringBuilder3:
4242
def apply(s: String): StringBuilder3 = new StringBuilder3(s)
4343
def apply(): StringBuilder3 = new StringBuilder3()
4444

45-
def no3() =
45+
def no3(): Unit =
4646
val sb1 = StringBuilder3("abc")
4747
val sb2 = StringBuilder3()
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
package com.github.shinharad.gettingStartedWithScala3
22
package varargSplices
33

4-
def varargSplices1 =
5-
val arr = Array(1, 2, 3)
6-
val lst = List(arr*) // vararg splice argument
4+
@main def no1(): Unit =
5+
val arr = Array(0, 1, 2, 3)
6+
val lst = List(arr*) // vararg splice argument
77
lst match
88
case List(0, 1, xs*) => println(xs) // binds xs to Seq(2, 3)
99
case List(1, _*) => // wildcard pattern
1010
case _ =>
1111

1212
// 今までの書き方は段階的に廃止される予定
13-
def varargSplices2 =
14-
val arr = Array(1, 2, 3)
13+
@main def no2(): Unit =
14+
val arr = Array(0, 1, 2, 3)
1515
val lst = List(arr: _*)
1616

1717
lst match
18-
case List(0, 1, xs: _*) => println(xs) // binds xs to Seq(2, 3)
18+
case List(0, 1, xs @ _*) => println(xs) // binds xs to Seq(2, 3)
1919
case _ =>

0 commit comments

Comments
 (0)