Skip to content

Instantly share code, notes, and snippets.

@paulp
Created October 1, 2010 05:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save paulp/605797 to your computer and use it in GitHub Desktop.
Save paulp/605797 to your computer and use it in GitHub Desktop.
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