Skip to content
This repository was archived by the owner on Mar 27, 2025. It is now read-only.

Commit d79c35b

Browse files
authored
Bump Scala version/revise samples/add scalafmt (#118)
1 parent 21ff3e0 commit d79c35b

File tree

25 files changed

+220
-136
lines changed

25 files changed

+220
-136
lines changed

code-snippets/.scalafmt.conf

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
version = 3.7.2
2+
runner.dialect = scala3
3+
4+
style = defaultWithAlign
5+
indentOperator.preset = akka
6+
maxColumn = 120
7+
rewrite.rules = [RedundantParens, AvoidInfix]
8+
align.tokens = [{code = "=>", owner = "Case"}]
9+
align.openParenDefnSite = false
10+
align.openParenCallSite = false
11+
optIn.breakChainOnFirstMethodDot = false
12+
optIn.configStyleArguments = false
13+
danglingParentheses.defnSite = false
14+
danglingParentheses.callSite = false
15+
rewrite.neverInfix.excludeFilters = [
16+
and
17+
min
18+
max
19+
until
20+
to
21+
by
22+
eq
23+
ne
24+
"should.*"
25+
"contain.*"
26+
"must.*"
27+
in
28+
ignore
29+
be
30+
taggedAs
31+
thrownBy
32+
synchronized
33+
have
34+
when
35+
size
36+
only
37+
noneOf
38+
oneElementOf
39+
noElementsOf
40+
atLeastOneElementOf
41+
atMostOneElementOf
42+
allElementsOf
43+
inOrderElementsOf
44+
theSameElementsAs
45+
message
46+
]
47+
rewriteTokens = {
48+
"⇒": "=>"
49+
"→": "->"
50+
"←": "<-"
51+
}

code-snippets/build.sbt

+5-9
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,19 @@
1-
21
Global / onChangedBuildSource := ReloadOnSourceChanges
32

43
lazy val `scala-3-snippets` = project
54
.in(file("."))
6-
.settings(ThisBuild / scalaVersion := "3.2.2")
5+
.settings(ThisBuild / scalaVersion := "3.3.0")
76
.settings(
87
name := "dotty-simple",
98
version := "0.1.0",
10-
11-
libraryDependencies += "com.novocode" % "junit-interface" % "0.11" % "test"
12-
)
9+
libraryDependencies += "com.novocode" % "junit-interface" % "0.11" % "test")
1310
.aggregate(
1411
`new-control-structure-syntax`,
1512
`contextual-abstractions`,
1613
`intersection-and-union-types`,
1714
`enumerations`,
1815
`export-clause`,
19-
`top-level-definitions`,
20-
)
16+
`top-level-definitions`)
2117

2218
lazy val `new-control-structure-syntax` = project.in(file("new-control-structure-syntax"))
2319

@@ -27,7 +23,7 @@ lazy val `enumerations` = project.in(file("enumerations"))
2723

2824
lazy val `export-clause` = project.in(file("export-clause"))
2925

30-
lazy val `intersection-and-union-types` = project.in(file("intersection-and-union-types"))
26+
lazy val `intersection-and-union-types` =
27+
project.in(file("intersection-and-union-types")).settings(scalacOptions ++= Seq("-source", "future-migration"))
3128

3229
lazy val `top-level-definitions` = project.in(file("top-level-definitions"))
33-
Original file line numberDiff line numberDiff line change
@@ -1,3 +1 @@
1-
scalacOptions ++= Seq(
2-
"-source:3.0",
3-
)
1+
scalacOptions ++= Seq("-source:3.0")

code-snippets/contextual-abstractions/src/main/scala/org/lunatech/dotty/multiversalequality/BadRepository.scala

+11-15
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,10 @@ package org.lunatech.dotty.multiversalequality
22

33
import java.util.UUID
44

5-
/**
6-
* In this example we imagine that we refactored 'Item' so that now we use
7-
* UUID instead of our custom-made 'Id' value class to identify items.
8-
* But in the refactor we forgot to change the Repository.findById.
9-
* This will type-check but we will not find the repository
10-
*/
5+
/** In this example we imagine that we refactored 'Item' so that now we use UUID instead of our custom-made 'Id' value
6+
* class to identify items. But in the refactor we forgot to change the Repository.findById. This will type-check but
7+
* we will not find the repository
8+
*/
119
object Bad1:
1210
final case class Id(value: Long) extends AnyVal
1311
final case class Item(id: UUID)
@@ -17,20 +15,18 @@ object Bad1:
1715
items.find(_.id == id)
1816
}
1917

20-
/**
21-
* Same as above but now we import strictEquality in the scope. This will now
22-
* fail to compile because we don't have any Eql typeclass instances for
23-
* comparing 'Id' with UUID
24-
*/
18+
/** Same as above but now we import strictEquality in the scope. This will now fail to compile because we don't have any
19+
* Eql typeclass instances for comparing 'Id' with UUID
20+
*/
2521
object Bad2:
2622

2723
final case class Id(value: Long) extends AnyVal
2824
final case class Item(id: UUID)
2925

3026
import scala.language.strictEquality
31-
/** COMMENTED OUT. DOES NOT COMPILE.
32-
* For illustration purposes
33-
*/
34-
// class Repository(items: Seq[Item]):
27+
28+
/** COMMENTED OUT. DOES NOT COMPILE. For illustration purposes
29+
*/
30+
// class Repository(items: Seq[Item]):
3531
// def findById(id: Id): Option[Item] =
3632
// items.find(_.id == id) // Thus futile comparison is flagged as an error

code-snippets/contextual-abstractions/src/main/scala/org/lunatech/dotty/opaquetypes/Units.scala

+9-18
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@ package org.lunatech.dotty.opaquetypes
33
object Scala2TypeAliases:
44
type Kilometres = Double
55
class Rocket(distanceTravelled: Kilometres):
6-
def advance(distanceToAdvance: Kilometres): Rocket = new Rocket(
7-
distanceTravelled + distanceToAdvance
8-
)
6+
def advance(distanceToAdvance: Kilometres): Rocket = new Rocket(distanceTravelled + distanceToAdvance)
97

108
type Miles = Double
119
class Booster():
@@ -18,19 +16,18 @@ object Scala2ClassWrappers:
1816
case class Kilometres(value: Double)
1917
class Rocket(distanceTravelled: Kilometres):
2018
def advance(distanceToAdvance: Kilometres): Rocket = new Rocket(
21-
Kilometres(distanceTravelled.value + distanceToAdvance.value)
22-
)
19+
Kilometres(distanceTravelled.value + distanceToAdvance.value))
2320

2421
case class Miles(value: Double)
2522
class Booster() {
23+
2624
/** COMMENTED OUT. DOES NOT COMPILE. */
2725

2826
// def advanceRocket(rocket: Rocket, distanceToAdvance: Miles): Rocket = {
2927
// // Kilometres and Miles are different types. So compiler prevents this bug
3028
// rocket.advance(distanceToAdvance)
3129
// }
3230

33-
3431
}
3532
end Scala2ClassWrappers
3633

@@ -137,7 +134,7 @@ object Scala3OpaqueTypeAliasesDefinitions:
137134
def toMiles: Miles = a / 1.6
138135

139136
extension (a: Miles)
140-
@targetName("plusMiles")
137+
@targetName("plusMiles")
141138
def +(b: Miles): Miles = a + b
142139

143140
def toKm: Kilometres = a * 1.6
@@ -148,9 +145,7 @@ object Scala3OpaqueTypeAliasesTypeSafety:
148145
import Scala3OpaqueTypeAliasesDefinitions._
149146

150147
class Rocket(distanceTravelled: Kilometres):
151-
def advance(distanceToAdvance: Kilometres): Rocket = new Rocket(
152-
distanceTravelled + distanceToAdvance
153-
)
148+
def advance(distanceToAdvance: Kilometres): Rocket = new Rocket(distanceTravelled + distanceToAdvance)
154149

155150
class Booster():
156151
def advanceRocket(rocket: Rocket, distanceToAdvance: Kilometres): Rocket = {
@@ -169,17 +164,15 @@ object Scala3OpaqueTypeAliasesNoAllocations1:
169164
export Scala3OpaqueTypeAliasesDefinitions._
170165

171166
class Rocket(val distanceTravelled: Kilometres):
172-
def advance(distanceToAdvance: Kilometres): Rocket = new Rocket(
173-
distanceTravelled + distanceToAdvance
174-
)
167+
def advance(distanceToAdvance: Kilometres): Rocket = new Rocket(distanceTravelled + distanceToAdvance)
175168

176169
type Distance = Kilometres | Miles
177170
class Booster():
178171
// THIS GIVES A WARNING. THE 'Kilometres' CASE IS UNREACHABLE due to erasure.
179172
// SO WE HAVE A BUG. Any 'Kilometres' passed to this method will be multiplied by 1.6
180173
def advanceRocket(rocket: Rocket, distanceToAdvance: Distance): Rocket = {
181174
val distanceInKm = distanceToAdvance match {
182-
case miles: Miles => miles.toKm
175+
case miles: Miles => miles.toKm
183176
case km: Kilometres => km
184177
}
185178
rocket.advance(distanceInKm)
@@ -202,9 +195,7 @@ object Scala3OpaqueTypeAliasesNoAllocations2:
202195
import Scala3OpaqueTypeAliasesDefinitions._
203196

204197
class Rocket(val distanceTravelled: Kilometres) {
205-
def advance(distanceToAdvance: Kilometres): Rocket = new Rocket(
206-
distanceTravelled + distanceToAdvance
207-
)
198+
def advance(distanceToAdvance: Kilometres): Rocket = new Rocket(distanceTravelled + distanceToAdvance)
208199
}
209200

210201
type Conversion[A] = A => Kilometres
@@ -239,6 +230,6 @@ object Scala3OpaqueTypeAliasesNoAllocations3:
239230
* public static double[] distances() {
240231
* return Scala3OpaqueTypeAliasesNoAllocations3$.MODULE$.distances();
241232
* }
242-
*
233+
*
243234
*/
244235
val distances: Array[Kilometres] = Array(Kilometres(10)) // No allocation of Kilometres object

code-snippets/contextual-abstractions/src/main/scala/org/lunatech/dotty/using/ContextBounds.scala

+5-5
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ package org.lunatech.dotty.using
33
trait Ord[T] {
44
def compare(x: T, y: T): Int
55
extension (x: T)
6-
def < (y: T) = compare(x, y) < 0
7-
def > (y: T) = compare(x, y) > 0
6+
def <(y: T) = compare(x, y) < 0
7+
def >(y: T) = compare(x, y) > 0
88
}
99

1010
given ordInt: Ord[Int] with {
@@ -16,10 +16,10 @@ object ContextBounds {
1616
if (x > y) x else y
1717

1818
// The following will still be allowed in Scala 3.0, but disallowed in 3.1
19-
//@main def contextBoundsMain = println(max(1,3)(ordInt))
19+
// @main def contextBoundsMain = println(max(1,3)(ordInt))
2020

2121
}
2222

2323
// If you want to explicitly pass an instance of Ord[Int], do it as follows
24-
@main def contextBoundsMain(): Unit =
25-
println(ContextBounds.max(1,3)(using ordInt))
24+
@main def contextBoundsMain(): Unit =
25+
println(ContextBounds.max(1, 3)(using ordInt))
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
package org.lunatech.dotty.using
22

3-
final case class Engine(name: String)
3+
final case class Engine(name: String)
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
package org.lunatech.dotty.using
22

3-
final case class EngineConfig(initialSpeed: Int, maxSpeed: Int, label: String)
3+
final case class EngineConfig(initialSpeed: Int, maxSpeed: Int, label: String)

code-snippets/contextual-abstractions/src/main/scala/org/lunatech/dotty/using/MultipleUsingClauses.scala

+5-8
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,7 @@ final case class RecordDev(recordType: String)
44
final case class PlayerDev(playerType: String)
55

66
object MultipleUsingClauses {
7-
def recordAndMonitor(recordGain: Int)
8-
(using recordDev: RecordDev)
9-
(volume: Int = 3)
10-
(using player: PlayerDev) = {
7+
def recordAndMonitor(recordGain: Int)(using recordDev: RecordDev)(volume: Int = 3)(using player: PlayerDev) = {
118
println(s"Recording with gain $recordGain from $recordDev to $player with volume $volume")
129
}
1310

@@ -27,8 +24,8 @@ object MultipleUsingClauses2 {
2724
opaque type Mul = Int
2825
opaque type Add = Int
2926

30-
object Mul { def apply(n: Int): Mul = n}
31-
object Add { def apply(n: Int): Add = n}
27+
object Mul { def apply(n: Int): Mul = n }
28+
object Add { def apply(n: Int): Add = n }
3229

3330
import scala.annotation.targetName
3431

@@ -39,7 +36,7 @@ object MultipleUsingClauses2 {
3936
extension (a: Add)
4037
@targetName("addAsInt")
4138
def asInt: Int = a
42-
39+
4340
@main def MUC_1: Unit = {
4441
val r = 5
4542
println(r)
@@ -57,4 +54,4 @@ object Use {
5754
val r = 10 * summon[Mul].asInt + summon[Add].asInt
5855
println(r)
5956
}
60-
}
57+
}

code-snippets/contextual-abstractions/src/main/scala/org/lunatech/dotty/using/UsingBaseSamples.scala

+4-4
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ object Scala2Example {
1212
@main def CA_0: Unit = {
1313
startEngine(Engine("AC-35-B/002"))
1414
}
15-
15+
1616
}
1717

1818
object MovingToDotty_1 {
@@ -25,7 +25,7 @@ object MovingToDotty_1 {
2525
@main def CA_1: Unit = {
2626
startEngine(Engine("AC-35-B/002"))
2727
}
28-
28+
2929
}
3030

3131
object MovingToDotty_2 {
@@ -38,5 +38,5 @@ object MovingToDotty_2 {
3838
@main def CA_2: Unit = {
3939
startEngine(Engine("AC-35-B/002"))
4040
}
41-
42-
}
41+
42+
}

code-snippets/enumerations/src/main/scala/org/lunatech/dotty/enumeration/EnumForProtocol.scala

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ object SomeActor {
1010
}
1111
export Command._
1212

13-
//def apply(): Behavior[Command] = ???
13+
// def apply(): Behavior[Command] = ???
1414
}
1515

16-
// someActor ! SomeActor.Command.Execute(5)
16+
// someActor ! SomeActor.Command.Execute(5)
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,39 @@
11
package org.lunatech.dotty.enumeration
22

33
@main def enumMemberVisibilityDefault = {
4-
4+
55
enum Command {
66
case Reset
77
case IncrementBy(inc: Int)
88
case Configure(init: Int, inc: Int)
99
}
1010

11-
val reset = Command.Reset // `reset` is a singleton case mapped to a val definition
12-
val inc = Command.IncrementBy(2) // `inc` is NOT a case class
11+
val reset = Command.Reset // `reset` is a singleton case mapped to a val definition
12+
val inc = Command.IncrementBy(2) // `inc` is NOT a case class
1313

1414
val r1 = Command.Reset
1515
val r2 = Command.Reset
1616

17-
println(r1 eq r2) // will print `true`
17+
println(r1 eq r2) // will print `true`
1818
println(s"reset.ordinal = ${reset.ordinal}") // will print reset.ordinal = 0
1919
println(s"inc.ordinal = ${inc.ordinal}")
2020

2121
}
2222

2323
@main def enumMemberVisibility = {
24-
24+
2525
enum Command {
2626
case Reset
2727
case IncrementBy(inc: Int)
2828
case Configure(init: Int, inc: Int)
2929
}
3030

31-
val inc = new Command.IncrementBy(2) // `inc` IS a case class
32-
val conf = new Command.Configure(0, 5) // `conf` IS a case class
31+
val inc = new Command.IncrementBy(2) // `inc` IS a case class
32+
val conf = new Command.Configure(0, 5) // `conf` IS a case class
3333

3434
println(s"inc.ordinal = ${inc.ordinal}")
35-
println(s"conf = $conf") // will print Configure(0, 5)
35+
println(s"conf = $conf") // will print Configure(0, 5)
3636
val conf1 = conf.copy(inc = 7)
37-
println(s"conf1 = $conf1") // will print Configure(0, 7)
38-
conf1 // which members are defined on this enum instance?
37+
println(s"conf1 = $conf1") // will print Configure(0, 7)
38+
conf1 // which members are defined on this enum instance?
3939
}

0 commit comments

Comments
 (0)