Skip to content

Latest commit

 

History

History
22 lines (14 loc) · 1.22 KB

union_type.adoc

File metadata and controls

22 lines (14 loc) · 1.22 KB

Union Type

Warning
This is incomplete and work in progress, refer to Miles' blog (linked below) for full details though :-)

Let’s start discussing this Type by remembering set theory, and viewing the already known construction A with B as "Intersection Type":

Why? Well, the only objects that conform to this type are those who have the type A and type B, so in set theory, this would be an intersection. On the other hand, let’s think what an Union Type is then:

It’s an union of these two sets, so set wise it’s an type A or type B. Our task at hand is to introduce such type using Scala’s type system. While not being a first-class construct in Scala (it’s not built in) they are pretty easy to implement and use ourselves. Miles Sabin explains this technique in depth in the blog post 'Unboxed union types in Scala via the Curry-Howard isomorphism' if you’re curious for an in-depth explanation.

type |∨|[T, U] = { type λ[X] = ¬¬[X] <:< (TU) }

def size[T : (Int || String)#λ](t : T) = t match {
    case i : Int => i
    case s : String => s.length
}