Created
January 25, 2021 23:06
-
-
Save Kakadu/8692e4aaa50215d54935ec9d8ee3d313 to your computer and use it in GitHub Desktop.
Scala 2 with or type
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
sealed trait AST | |
case class ConstInt(x: Int) extends AST | |
case class ConstDouble(x: Double) extends AST | |
case class SomethingElse() extends AST | |
type ¬[A] = A => Nothing | |
type ∨[T, U] = ¬[¬[T] with ¬[U]] | |
type ¬¬[A] = ¬[¬[A]] | |
type |∨|[T, U] = { type λ[X] = ¬¬[X] <:< (T ∨ U) } | |
def size[T: (Int |∨| String)#λ](t: T) = | |
t match { | |
case i: Int => i | |
case s: String => s.length | |
} | |
object ANumber { | |
def unapply[T : (Int |∨| String)#λ](ast: AST): Option[T] = { | |
ast match { | |
case ConstInt(n: Int)=> Some(n) | |
/* | |
type mismatch; | |
found : n.type (with underlying type Int) | |
required: T | |
*/ | |
case ConstDouble(n)=> Some(n) | |
case _ => None | |
} | |
} | |
} | |
def interpret(x: AST) = { | |
x match { | |
case ANumber(int_or_double) => "number" | |
case SomethingElse() => "???" | |
} | |
} |
bash-spbu
commented
Jan 26, 2021
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment