Created
October 1, 2010 05:41
-
-
Save paulp/605797 to your computer and use it in GitHub Desktop.
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
object Wat { | |
trait TypeSum[T, U] { | |
type SumType | |
def add(t: T, u: U): SumType | |
} | |
implicit def unitLeft[T]: TypeSum[Unit, T] = new TypeSum[Unit, T] { | |
type SumType = T | |
def add(u: Unit, t: T) = t | |
} | |
implicit def unitRight[T]: TypeSum[T, Unit] = new TypeSum[T, Unit] { | |
type SumType = T | |
def add(t: T, u: Unit) = t | |
} | |
implicit def anySum[T, X]: TypeSum[T, X] = new TypeSum[T, X] { | |
type SumType = (T, X) | |
def add(t: T, x: X) = (t, x) | |
} | |
def combine[A, B](a: A, b: B)(implicit ta: TypeSum[A, B]) = ta.add(a,b) | |
assert(combine(combine((), 3), ()) == 3) // Not a problem | |
val t = combine(combine((), 3), ()) // Doesn't compile like a boss. | |
} | |
% scalac -Xexperimental bip.scala | |
% scala -e 'println(Wat.t)' | |
3 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment