Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@

import scala.concurrent.{ExecutionContext, Future}
import scala.util.Try

/**
* Context Queries:
* - http://dotty.epfl.ch/docs/reference/contextual/query-types.html,
* Context Functions:
* - https://dotty.epfl.ch/docs/reference/contextual/context-functions.html
* - https://www.scala-lang.org/blog/2016/12/07/implicit-function-types.html
*/
object ContextQueries /* Formerly known as Implicit Function Types */ {
object ContextFunctions {

object context {
// type alias Contextual
Expand All @@ -21,10 +20,10 @@ object ContextQueries /* Formerly known as Implicit Function Types */ {

object parse {

type Parseable[T] = ImpliedInstances.StringParser[T] ?=> Try[T]
type Parseable[T] = GivenInstances.StringParser[T] ?=> Try[T]

def sumStrings(x: String, y: String): Parseable[Int] = {
val parser = implicitly[ImpliedInstances.StringParser[Int]]
val parser = summon[GivenInstances.StringParser[Int]]
val tryA = parser.parse(x)
val tryB = parser.parse(y)

Expand All @@ -36,7 +35,6 @@ object ContextQueries /* Formerly known as Implicit Function Types */ {
}

def test: Unit = {

import ExecutionContext.Implicits.global
context.asyncSum(3, 4).foreach(println)
context.asyncMult(3, 4).foreach(println)
Expand Down
2 changes: 0 additions & 2 deletions src/main/scala/Conversion.scala
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,4 @@ object Conversion {
println(convert(new IntWrapper(42)))
}



}
1 change: 0 additions & 1 deletion src/main/scala/EnumTypes.scala
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ object EnumTypes {
}

def test: Unit = {

val emptyList = ListEnum.Empty
val list = ListEnum.Cons(1, ListEnum.Cons(2, ListEnum.Cons(3, ListEnum.Empty)))
println(emptyList)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import scala.util.{Success, Try}

/**
* Implied Instances:
* - https://dotty.epfl.ch/docs/reference/contextual/instance-defs.html
* Implied Instances: https://dotty.epfl.ch/docs/reference/contextual/givens.html
*/
object ImpliedInstances {
object GivenInstances {

sealed trait StringParser[A] {
def parse(s: String): Try[A]
Expand All @@ -30,10 +29,11 @@ object ImpliedInstances {
}

def test: Unit = {
println(implicitly[StringParser[Option[Int]]].parse("21"))
println(implicitly[StringParser[Option[Int]]].parse(""))
println(implicitly[StringParser[Option[Int]]].parse("21a"))
println(summon[StringParser[Option[Int]]].parse("21"))
println(summon[StringParser[Option[Int]]].parse(""))
println(summon[StringParser[Option[Int]]].parse("21a"))

println(implicitly[StringParser[Option[Int]]](StringParser.optionParser[Int]).parse("42"))
println(summon[StringParser[Option[Int]]](using StringParser.optionParser[Int]).parse("42"))
}

}
3 changes: 1 addition & 2 deletions src/main/scala/IntersectionTypes.scala
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,13 @@ object IntersectionTypes {
}

def test: Unit = {

def euclideanDistance(p1: X & Y, p2: X & Y) = {
Math.sqrt(Math.pow(p2.y - p1.y, 2) + Math.pow(p2.x - p1.x, 2))
}

val p1: P = Point(3, 4)
val p2: PP = Point(6, 8)
println(euclideanDistance(p1, p2))

}

}
7 changes: 3 additions & 4 deletions src/main/scala/Main.scala
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

object Main {

def main(args: Array[String]): Unit = {
Expand All @@ -7,9 +6,9 @@ object Main {

runExample("Enum Types")(EnumTypes.test)

runExample("Context Queries")(ContextQueries.test)
runExample("Context Functions")(ContextFunctions.test)

runExample("Implied Instances")(ImpliedInstances.test)
runExample("Given Instances")(GivenInstances.test)

runExample("Conversion")(Conversion.test)

Expand All @@ -21,7 +20,7 @@ object Main {

runExample("Multiversal Equality")(MultiversalEquality.test)

runExample("Auto Param Tupling")(AutoParamTupling.test)
runExample("Parameter Untupling")(ParameterUntupling.test)

runExample("Structural Types")(StructuralTypes.test)

Expand Down
2 changes: 1 addition & 1 deletion src/main/scala/MultiversalEquality.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import scala.language.strictEquality
object MultiversalEquality {

def test: Unit = {

// Values of types Int and String cannot be compared with == or !=,
// unless we add the derived delegate instance like:
given CanEqual[Int, String] = CanEqual.derived
Expand Down Expand Up @@ -35,4 +34,5 @@ object MultiversalEquality {
println(a != b)
println(b == a)
}

}
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@

/**
* Automatic Tupling of Function Params: https://dotty.epfl.ch/docs/reference/other-new-features/auto-parameter-tupling.html
* Parameter Untupling: https://dotty.epfl.ch/docs/reference/other-new-features/parameter-untupling.html
*/
object AutoParamTupling {
object ParameterUntupling {

def test: Unit = {

/**
* In order to get thread safety, you need to put @volatile before lazy vals.
* https://dotty.epfl.ch/docs/reference/changed-features/lazy-vals.html
Expand All @@ -19,6 +17,6 @@ object AutoParamTupling {
* Consider a pattern matching anonymous function, `{ case (s, i) => ... }`
*/
xs.zipWithIndex.map((s, i) => println(s"$i: $s"))

}

}
17 changes: 7 additions & 10 deletions src/main/scala/PatternMatching.scala
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

/**
* Pattern Matching: https://dotty.epfl.ch/docs/reference/changed-features/pattern-matching.html
*/
Expand Down Expand Up @@ -59,19 +58,17 @@ object PatternMatching {
}

def test: Unit = {

import booleanPattern._

"even" match {
case s @ Even() => println(s"$s has an even number of characters")
case s => println(s"$s has an odd number of characters")
case s => println(s"$s has an odd number of characters")
}

// http://dotty.epfl.ch/docs/reference/changed/vararg-patterns.html
// http://dotty.epfl.ch/docs/reference/changed-features/vararg-patterns.html
def containsConsecutive(list: List[Int]): Boolean = list match {
case List(a, b, xs: _ *) => if (a == b) true else containsConsecutive(b :: xs.toList)
case List(a, _ : _*) => false
case Nil => false
case List(a, b, xs: _*) => if (a == b) true else containsConsecutive(b :: xs.toList)
case Nil | List(_, _: _*) => false
}

println(containsConsecutive(List(1, 2, 3, 4, 5)))
Expand All @@ -86,7 +83,7 @@ object PatternMatching {

def greet(fullName: String) = fullName match {
case Names(lastName, firstName, _: _*) => "Good morning, " + firstName + " " + lastName + "!"
case _ => "Welcome! Please make sure to fill in your name!"
case _ => "Welcome! Please make sure to fill in your name!"
}

println(greet("Alan Turing"))
Expand All @@ -96,8 +93,8 @@ object PatternMatching {
import namePattern._
"alice" match {
case Name(n) => println(s"name is $n")
case _ => println("empty name")
case _ => println("empty name")
}

}

}
2 changes: 1 addition & 1 deletion src/main/scala/StructuralTypes.scala
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

/**
* Structural Types: https://dotty.epfl.ch/docs/reference/changed-features/structural-types.html
*/
Expand All @@ -25,4 +24,5 @@ object StructuralTypes {
// age field is java.util.NoSuchElementException: None.get
//println(invalidPerson.age)
}

}
2 changes: 1 addition & 1 deletion src/main/scala/TraitParams.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ object TraitParams {
private def printMessages(msgs: (A | B)*) = println(msgs.map(_.msg).mkString(" "))

def test: Unit = {

printMessages(new A, new B)

// Sanity check the classpath: this won't run if the dotty jar is not present.
val x: Int => Int = z => z
x(1)
}

}
1 change: 0 additions & 1 deletion src/main/scala/TypeLambdas.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ object TypeLambdas {
type Tuple = [X] =>> (X, X)

def test: Unit = {

val m: T[String, Int] = Map(1 -> "1")
println(m)

Expand Down
11 changes: 5 additions & 6 deletions src/main/scala/UnionTypes.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ object UnionTypes {
type DivisionResult = DivisionByZero | Success

sealed trait List[+A]
final case class Empty() extends List[Nothing]
case object Empty extends List[Nothing]
final case class Cons[+A](h: A, t: List[A]) extends List[A]

private def safeDivide(a: Double, b: Double): DivisionResult = {
Expand All @@ -20,11 +20,10 @@ object UnionTypes {

private def either(division: Division) = division match {
case DivisionByZero(m) => Left(m)
case Success(d) => Right(d)
case Success(d) => Right(d)
}

def test: Unit = {

val divisionResultSuccess: DivisionResult = safeDivide(4, 2)

// commutative
Expand All @@ -36,10 +35,10 @@ object UnionTypes {
// calling `either` function with union typed value.
println(either(divisionResultFailure))

val list: Cons[Int] | Empty = Cons(1, Cons(2, Cons(3, Empty())))
val emptyList: Empty | Cons[Any] = Empty()
val list: Cons[Int] | Empty.type = Cons(1, Cons(2, Cons(3, Empty)))
val emptyList: Empty.type | Cons[Any] = Empty
println(list)
println(emptyList)

}

}