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

Commit 71b8711

Browse files
authored
Bump code-snippets to Scala 3.0.0/Clean-up (#78)
- Bump to Scala 3.0.0 - Bump to sbt 1.5.2 and clean-up build - Adapt sample code to align with final Scala 3 syntax - Duplicate some worksheets to Scala source files to demonstrate aspects of generated code
1 parent 4f808e3 commit 71b8711

23 files changed

+499
-297
lines changed

code-snippets/build.sbt

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,13 @@
1-
val dottyVersion = "0.27.0-RC1"
21

32
Global / onChangedBuildSource := ReloadOnSourceChanges
43

54
lazy val `dotty-snippets` = project
65
.in(file("."))
7-
.settings(ThisBuild / scalaVersion := dottyVersion)
6+
.settings(ThisBuild / scalaVersion := "3.0.0-RC3")
87
.settings(
98
name := "dotty-simple",
109
version := "0.1.0",
1110

12-
//ThisBuild / scalaVersion := dottyVersion,
13-
ThisBuild / scalaVersion := dottyLatestNightlyBuild.get,
14-
1511
libraryDependencies += "com.novocode" % "junit-interface" % "0.11" % "test"
1612
)
1713
.aggregate(

code-snippets/contextual-abstractions/src/main/scala/org/lunatech/dotty/extensionmethods/CollectiveExtensions.scala

Lines changed: 0 additions & 18 deletions
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Metals Scala worksheet
2+
3+
extension (i: Int)
4+
def isEven: Boolean = i % 2 == 0
5+
6+
def unary_! : Int = -i
7+
8+
def square : Int = i * i
9+
10+
extension [T](xs: List[T])
11+
def second: T = xs.tail.head
12+
def third: T = xs.tail.second
13+
14+
println(List(1,2,3).second)
15+
println(! 5)
16+
println(5.isEven)
17+
println(5.square)

code-snippets/contextual-abstractions/src/main/scala/org/lunatech/dotty/extensionmethods/ExtensionOperators.scala

Lines changed: 0 additions & 9 deletions
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// Metals Scala worksheet
2+
3+
extension (a: String) def < (b: String): Boolean = a.compareTo(b) < 0
4+
5+
extension (a: Int) def +++: (b: List[Int]) = a::b
6+
7+
println("abc" < "pqr")
8+
println(1 +++: List(2,3,4))
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
package org.lunatech.dotty.extensionmethods
1+
// Metals Scala worksheet
2+
3+
import scala.annotation.targetName
24

35
extension [T](xs: List[T])
46
def second =
@@ -9,13 +11,17 @@ extension [T](xs: List[List[T]])
911
xs.foldLeft[List[T]](Nil)(_ ++ _)
1012

1113
extension [T: Numeric](x: T)
12-
def + (y: T): T =
14+
def +++ (y: T): T =
1315
summon[Numeric[T]].plus(x, y)
16+
def --- (y: T): T =
17+
summon[Numeric[T]].minus(x, y)
1418

15-
def [T: Numeric](x: T) - (y: T) = summon[Numeric[T]].plus(x, y)
19+
1 --- 2
20+
2 +++ 1
1621

1722
case class Circle(x: Double, y: Double, radius: Double)
18-
def (c: Circle).circumference: Double = c.radius * math.Pi * 2
1923

20-
@main def circles() =
21-
println(s"Circle: ${Circle(1.0, 2.0, 4).circumference}")
24+
extension (c: Circle)
25+
def circumference: Double = c.radius * math.Pi * 2
26+
27+
Circle(1.0, 2.0, 4).circumference

code-snippets/contextual-abstractions/src/main/scala/org/lunatech/dotty/extensionmethods/IntOps.scala

Lines changed: 0 additions & 20 deletions
This file was deleted.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// Metals Scala worksheet
2+
extension (i: Int)
3+
def isEven: Boolean = i % 2 == 0
4+
5+
def unary_! : Int = -i
6+
7+
println(3.isEven)
8+
println(!5)
9+
Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
trait IntOps:
2-
extension (i: Int) def isZero: Boolean = i == 0
2+
extension (i: Int)
3+
def isZero: Boolean = i == 0
34

4-
extension (i: Int) def safeMod(x: Int): Option[Int] =
5-
// extension method defined in same scope IntOps
6-
if x.isZero then None
7-
else Some(i % x)
5+
def safeMod(x: Int): Option[Int] =
6+
// extension method defined in same scope IntOps
7+
if x.isZero then None
8+
else Some(i % x)
89

910
object IntOpsEx extends IntOps:
1011
extension (i: Int) def safeDiv(x: Int): Option[Int] =
@@ -20,30 +21,19 @@ object SafeDiv:
2021
(i.safeDiv(d), i.safeMod(d)) match
2122
case (Some(d), Some(r)) => Some((d, r))
2223
case _ => None
23-
24-
val d1 = 25.divide(3)
25-
val d2 = 25.divide(0)
2624

27-
SafeDiv.d1
28-
SafeDiv.d2
25+
import SafeDiv._
26+
val n = 9
27+
val d1 = 25.divide(3)
28+
val d2 = 25.divide(0)
2929

30-
given IntOps
30+
given IntOps = new IntOps{}
3131

3232
20.safeMod(0)
3333
20.safeMod(3)
3434

3535
extension [T](xs: List[List[T]])
3636
def flatten: List[T] = xs.foldLeft(Nil: List[T])(_ ++ _)
3737

38-
given [T: Ordering] as Ordering[List[T]]:
39-
override def compare(l1: List[T], l2: List[T]): Int = 1
40-
extension (xs: List[T])
41-
def < (ys: List[T]): Boolean = summon[Ordering[T]].lt(xs.head, ys.head)
42-
43-
4438
// extension method available since it is in the implicit scope of List[List[Int]]
4539
List(List(1, 2), List(3, 4)).flatten
46-
47-
// extension method available since it is in the given Ordering[List[T]],
48-
// which is itself in the implicit scope of List[Int]
49-
List(1, 2) < List(3)
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,17 @@
1-
package org.lunatech.dotty.givens
21

32
sealed trait Engine
43
final case class CarEngine(cylinder: Int) extends Engine
54
final case class TruckEngine(cylinder: Int) extends Engine
65

7-
trait Starter[T] {
6+
trait Starter[T]:
87
def start(e: T): Unit
9-
}
108

11-
given Starter[CarEngine] {
9+
given Starter[CarEngine] with {
1210
override def start(engine: CarEngine): Unit =
1311
println(s"Starting CarEngine with ${engine.cylinder} cylinder(s)")
1412
}
1513

16-
given Starter[TruckEngine] {
14+
given Starter[TruckEngine] with {
1715
override def start(engine: TruckEngine): Unit =
1816
println(s"Starting TruckEngine with ${engine.cylinder} cylinder(s)")
1917
}
@@ -23,9 +21,6 @@ def startEngine[E <: Engine: Starter](engine: E): Unit = {
2321
starter.start(engine)
2422
}
2523

26-
@main def testSummon(): Unit = {
27-
28-
startEngine(CarEngine(6))
29-
startEngine(TruckEngine(8))
30-
startEngine(TruckEngine(1))(using summon[Starter[TruckEngine]])
31-
}
24+
startEngine(CarEngine(6))
25+
startEngine(TruckEngine(8))
26+
startEngine(TruckEngine(1))(using summon[Starter[TruckEngine]])

0 commit comments

Comments
 (0)