-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add more snippets/Remove unused code in root folder (#120)
- Loading branch information
Showing
11 changed files
with
134 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
...ippets/opaque-type-aliases/src/main/scala/opaquetypealiases/caseclasses/CaseClasses.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
21 changes: 21 additions & 0 deletions
21
...que-type-aliases/src/main/scala/opaquetypealiases/opaquetypealias/OpaqueTypeAliases.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
26 changes: 26 additions & 0 deletions
26
...paque-type-aliases/src/main/scala/opaquetypealiases/opaquetypealias/UsingTheAliases.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
} |
25 changes: 25 additions & 0 deletions
25
...ases/src/main/scala/opaquetypealiases/parametricpolymorphism/ParametricPolymorphism.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
25 changes: 25 additions & 0 deletions
25
code-snippets/opaque-type-aliases/src/main/scala/opaquetypealiases/subtyping/Subtyping.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
11 changes: 11 additions & 0 deletions
11
...pets/opaque-type-aliases/src/main/scala/opaquetypealiases/valueclasses/ValueClasses.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
11 changes: 0 additions & 11 deletions
11
code-snippets/src/main/scala/org/lunatech/dotty/toplevel/TemperatureConverter.scala
This file was deleted.
Oops, something went wrong.
26 changes: 0 additions & 26 deletions
26
code-snippets/src/main/scala/org/lunatech/dotty/toplevel/TemperatureOps.scala
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters