This file contains hidden or 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
scala> case class Person(name: String, age: Int): | |
| def this(name:String) = this(name, 18) | |
// defined case class Person | |
scala> Person("Dean", 100) | |
val res2: Person = Person(Dean,100) | |
scala> Person("Dean") | |
1 |Person("Dean") | |
|^^^^^^^^^^^^^^ |
This file contains hidden or 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
scala> class JavaPerson(val name: String, val age: Int): | |
| def this(name:String) = this(name, 18) | |
// defined class JavaPerson | |
scala> JavaPerson("Dean", 100) | |
| JavaPerson("Dean") | |
| | |
val res0: JavaPerson = JavaPerson@6684da5b | |
val res1: JavaPerson = JavaPerson@34b156ba |
This file contains hidden or 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
import scala.compiletime.ops.int.* | |
// Only allowed values are Min <= N <= Max. | |
type Bounded[MIN <: Int, MAX <: Int] <: Int = MAX match | |
case MIN => MIN | |
case ? => MAX | Bounded[MIN,MAX-1] | |
val zero15: Bounded[1,5] = 0 // ERROR | |
val one15: Bounded[1,5] = 1 | |
val two15: Bounded[1,5] = 2 |
This file contains hidden or 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
scala> val s = Seq(1.1, "two", 3, BigDecimal(4.4)) | |
val s: Seq[Matchable] = List(1.1, two, 3, 4.4) | |
scala> s.foldLeft(DTNil)((l,x) => x +: l) | |
1 |s.foldLeft(DTNil)((l,x) => x +: l) | |
| ^^^^^^ | |
| Found: DTNonEmptyList[(0 : Int), Matchable, (l : DTNil.type)] | |
| Required: DTNil.type | |
scala> val seed: DTList[0] = DTNil |
This file contains hidden or 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
scala> val list = 1 +: "two" +: DTNil | |
val list: DTNonEmptyList[1, Int, ? <: DTNonEmptyList[0, String, DTNil.type]] = | |
DTNonEmptyList(1,DTNonEmptyList(two,DTNil)) | |
scala> list.size | |
| list.head | |
| list.tail | |
val res0: Int = 2 | |
val res1: Int = 1 | |
val res2: list.T = DTNonEmptyList(two,DTNil) |
This file contains hidden or 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
scala> val zero0: 0 = 0 | |
val zero0: 0 = 0 | |
scala> val zero1: 0 = 1 | |
1 |val zero1: 0 = 1 | |
| ^ | |
| Found: (1 : Int) | |
| Required: (0 : Int) | |
scala> val one0: 1 = 0 |
This file contains hidden or 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
// Adapted from | |
// https://github.com/deanwampler/programming-scala-book-code-examples/blob/master/src/script/scala/progscala3/typesystem/deptypes/DependentTypesSimple.scala | |
import scala.compiletime.ops.string.* // Scala 2 uses _ as the wild card, which you can still use | |
val s1: "ab" + "cd" = "abcd" | |
val bad2: "ab" + "cd" = "abcdef" // ERROR |
This file contains hidden or 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
// Adapted from | |
// https://github.com/deanwampler/programming-scala-book-code-examples/blob/master/src/script/scala/progscala3/typesystem/deptypes/DependentTypesSimple.scala | |
import scala.compiletime.ops.boolean.* // Scala 2 uses _ as the wild card, which you can still use | |
val t1: true = true | |
val f1: ![true] = false // Note how the left-hand size has to be written | |
val tt1: true && true = true | |
val tf1: true && false = false | |
val ft1: false && true = false |
This file contains hidden or 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
// Adapted from | |
// https://github.com/deanwampler/programming-scala-book-code-examples/blob/master/src/script/scala/progscala3/typesystem/deptypes/DependentTypesSimple.scala | |
import scala.compiletime.ops.int.S // In 3.0.0, it's scala.compiletime.S !! | |
val s1: S[0] = 1 | |
val s2a: S[S[0]] = 2 | |
val s2b: S[1] = 2 | |
val s3a: S[S[S[0]]] = 3 | |
val s3b: S[2] = 3 |
This file contains hidden or 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
// Adapted from | |
// https://github.com/deanwampler/programming-scala-book-code-examples/blob/master/src/script/scala/progscala3/typesystem/deptypes/DependentTypesSimple.scala | |
import scala.compiletime.ops.int.* // Scala 2 uses _ as the wild card, which you can still use | |
val i1: 0 + 1 = 0 + 1 | |
val i2: 1 + 1 = 1 + 1 | |
val i3: 1 + 2 = 1 + 2 | |
val i4: 3 * 2 - 1 = 3 * 2 - 1 | |
val i5: 12 / 3 = 4 |