Skip to content

Commit

Permalink
Add more snippets/Remove unused code in root folder (#120)
Browse files Browse the repository at this point in the history
  • Loading branch information
eloots authored Sep 1, 2023
1 parent d79c35b commit 258dc3b
Show file tree
Hide file tree
Showing 11 changed files with 134 additions and 44 deletions.
8 changes: 6 additions & 2 deletions code-snippets/build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ Global / onChangedBuildSource := ReloadOnSourceChanges
lazy val `scala-3-snippets` = project
.in(file("."))
.settings(ThisBuild / scalaVersion := "3.3.0")
.settings(ThisBuild / scalacOptions ++= Seq("-source", "3.3-migration"))
.settings(
name := "dotty-simple",
version := "0.1.0",
Expand All @@ -13,7 +14,8 @@ lazy val `scala-3-snippets` = project
`intersection-and-union-types`,
`enumerations`,
`export-clause`,
`top-level-definitions`)
`top-level-definitions`,
`opaque-type-aliases`)

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

Expand All @@ -24,6 +26,8 @@ lazy val `enumerations` = project.in(file("enumerations"))
lazy val `export-clause` = project.in(file("export-clause"))

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

lazy val `top-level-definitions` = project.in(file("top-level-definitions"))

lazy val `opaque-type-aliases` = project.in(file("opaque-type-aliases"))
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ object NewControlStructures {
val xs = Seq(1, 2, 3)
val ys = Seq(3, 4, 5)

def body: Int = 6

if x < 0 then "negative"
else if x == 0 then "zero"
else "positive"
Expand All @@ -26,6 +24,12 @@ object NewControlStructures {
y <- ys
do println(x + y)

// try body
// catch case ex: IOException => handle
def body: Int =
throw new IllegalArgumentException("Bad argument")
5

def handle = ???
val result =
try body
catch case ex: IllegalArgumentException => handle
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package opaquetypelaliases.caseclasses

case class Kilometres(value: Double)

class Rocket(distanceTravelled: Kilometres):
def advance(distanceToAdvance: Kilometres): Rocket = new Rocket(
Kilometres(distanceTravelled.value + distanceToAdvance.value))

val rocket = Rocket(Kilometres(1000))

val r1 = rocket.advance(Kilometres(5000))
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package opaquetypelaliases.opaquetypealias

object OpaqueTypeAliasesDefinitions:

opaque type Kilometres = Double
object Kilometres:
def apply(d: Double): Kilometres = d

opaque type Miles = Double
object Miles:
def apply(d: Double): Miles = d

extension (a: Kilometres)
@scala.annotation.targetName("plusKm")
def +(b: Kilometres): Kilometres = a + b
def toMiles: Miles = a / 1.6

extension (a: Miles)
@scala.annotation.targetName("plusMiles")
def +(b: Miles): Miles = a + b
def toKm: Kilometres = a * 1.6
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package opaquetypelaliases.opaquetypealias

object UsingTheAliases {
import OpaqueTypeAliasesDefinitions.*

class Rocket(distanceTravelled: Kilometres):
def advance(distanceToAdvance: Kilometres): Rocket = new Rocket(distanceTravelled + distanceToAdvance)

type Conversion[A] = A => Kilometres
class Booster():
def advanceRocket[A: Conversion](rocket: Rocket, distanceToAdvance: A): Rocket = {
val distanceInKm = summon[Conversion[A]](distanceToAdvance)
rocket.advance(distanceInKm)
}

val rocket1 = new Rocket(Kilometres(0))
val rocket2 = new Rocket(Kilometres(0))
val booster = new Booster()

given Conversion[Kilometres] = identity
given Conversion[Miles] = _.toKm

val r1 = booster.advanceRocket(rocket1, Kilometres(100)) // No allocation of Kilometres object
val r2 = booster.advanceRocket(rocket2, Miles(200)) // No allocation of Miles object

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package opaquetypelaliases.parametricpolymorphism

case class Kilometres(value: Double) extends AnyVal
case class Miles(value: Double) extends AnyVal

class Rocket(distanceTravelled: Kilometres):
def advance(distanceToAdvance: Kilometres): Rocket = new Rocket(
Kilometres(distanceTravelled.value + distanceToAdvance.value))

type Conversion[A] = A => Kilometres
class Booster():
def advanceRocket[A: Conversion](rocket: Rocket, distanceToAdvance: A): Rocket = {
val distanceInKm = summon[Conversion[A]](distanceToAdvance)
rocket.advance(distanceInKm)
}

val rocket1 = new Rocket(Kilometres(0))
val rocket2 = new Rocket(Kilometres(0))
val booster = new Booster()

given Conversion[Kilometres] = identity
given Conversion[Miles] = miles => Kilometres(miles.value * 1.6)

val r1 = booster.advanceRocket(rocket1, Kilometres(100)) // Allocation of Kilometres object
val r2 = booster.advanceRocket(rocket2, Miles(200))
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package opaquetypelaliases.subtyping

sealed trait Distance extends Any
case class Kilometres(value: Double) extends AnyVal with Distance
case class Miles(value: Double) extends AnyVal with Distance

class Rocket(distanceTravelled: Kilometres):
def advance(distanceToAdvance: Kilometres): Rocket = new Rocket(
Kilometres(distanceTravelled.value + distanceToAdvance.value))

class Booster():
def advanceRocket(rocket: Rocket, distanceToAdvance: Distance): Rocket = {
val distanceInKm = distanceToAdvance match {
case miles: Miles => Kilometres(miles.value * 1.6)
case km: Kilometres => km
}
rocket.advance(distanceInKm)
}

val rocket1 = new Rocket(Kilometres(0))
val rocket2 = new Rocket(Kilometres(0))
val booster = new Booster()

val r1 = booster.advanceRocket(rocket1, Kilometres(100)) // Allocation of Kilometres object
val r2 = booster.advanceRocket(rocket2, Miles(200)) // Allocation of Miles object
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package opaquetypelaliases.valueclasses

case class Kilometres(value: Double) extends AnyVal

class Rocket(distanceTravelled: Kilometres):
def advance(distanceToAdvance: Kilometres): Rocket = new Rocket(
Kilometres(distanceTravelled.value + distanceToAdvance.value))

val rocket1 = new Rocket(Kilometres(0))

val rocket2 = rocket1.advance(Kilometres(12000))

This file was deleted.

This file was deleted.

2 changes: 1 addition & 1 deletion slides/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=yes">

<title>Moving from Scala to Java</title>
<title>Moving from Scala 2 to Scala 3</title>

<link rel="stylesheet" href="dist/reset.css">
<link rel="stylesheet" href="dist/reveal.css">
Expand Down

0 comments on commit 258dc3b

Please sign in to comment.