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
val f123a: (T1 | T2 | T3) => String = _ match | |
case t1: T1 => "T1" | |
case t2: T2 => "T2" | |
case t3: T3 => "T3" | |
val f123b: (T1 => String) & (T2 => String) & (T3 => String) = f123a | |
val t123s: Seq[T1 | T2 | T3] = Seq(new T1 {}, new T2 {}, new T3 {}) | |
t123s.map(f123a) | |
t123s.map(f123b) |
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
// Covariance: | |
val t1s: Seq[T1] = Seq(new T1 {}) | |
val t2s: Seq[T2] = Seq(new T2 {}) | |
val t3s: Seq[T3] = Seq(new T3 {}) | |
val t123s1: Seq[T1 | T2 | T3] = t1s | |
val t123s2: Seq[T1 | T2 | T3] = t2s | |
val t123s3: Seq[T1 | T2 | T3] = t3s | |
// Contravariance? |
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
val t1: T1 = new T1 {} | |
val t2: T2 = new T2 {} | |
val t3: T3 = new T3 {} | |
val t123a: T1 | T2 | T3 = t1 | |
val t123b: T1 | T2 | T3 = t2 | |
val t123c: T1 | T2 | T3 = t3 |
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
// Adapted from "Programming Scala, Third Edition" (http://programming-scala.com) | |
// https://github.com/deanwampler/programming-scala-book-code-examples/blob/master/src/main/scala/progscala3/meta/Invariant.scala | |
import scala.quoted.* | |
object invariantMinInline: | |
val ignore = false | |
inline def apply[T]( // Use of "minimal" inlining. Does it still work? | |
predicate: => Boolean, // Now we need by-name parameters! | |
message: => String = "")( |
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
// Adapted from "Programming Scala, Third Edition" (http://programming-scala.com) | |
// https://github.com/deanwampler/programming-scala-book-code-examples/blob/master/src/main/scala/progscala3/meta/Invariant.scala | |
import scala.quoted.* | |
object invariantDisabled: | |
inline val ignore = true | |
inline def apply[T]( | |
inline predicate: Boolean, | |
inline message: String = "")( |
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
// Adapted from "Programming Scala, Third Edition" (http://programming-scala.com) | |
// https://github.com/deanwampler/programming-scala-book-code-examples/blob/master/src/main/scala/progscala3/meta/Invariant.scala | |
import scala.quoted.* | |
object invariantEnabled: | |
inline val ignore = false | |
inline def apply[T]( | |
inline predicate: Boolean, | |
inline message: String = "")( |
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
package progscala3.erased | |
import scala.language.experimental.erasedDefinitions | |
@main def Main() = | |
// Use the +: methods to construct sequences. | |
val nes1 = "one" +: EmptySeq | |
val nes2 = "two" +: nes1 | |
val nes3 = "three" +: nes2 |
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
package progscala3.erased | |
import scala.annotation.implicitNotFound | |
import scala.language.experimental.erasedDefinitions | |
sealed trait Emptiness | |
final class Empty extends Emptiness | |
final class NotEmpty extends Emptiness | |
@implicitNotFound("The seq must be empty") |
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
// Requires a snapshot or nightly build of Scala | |
val scala3 = "3.1.3-RC1-bin-SNAPSHOT" | |
lazy val root = project | |
.in(file(".")) | |
.settings( | |
name := "erased-feature-example", | |
description := "An example of the experimental 'erased' feature.", | |
version := "0.0.1", | |
scalaVersion := scala3, |
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
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 |
NewerOlder