September 22, 1999 Strong Typing Slide #20

Union Type Sabotage

Let's try to store a real into a MyNum and read it back as an int:

        fun intval (IV x) = x;
                Warning: match nonexhaustive
                  IV x => ...
                val intval = fn : MyNum -> int
        intval (FV 3.5);
                uncaught exception Match...

You can make a union type, but you can't store one type and get it back as another type

intval extracts the integer member from a MyNum, but only in a type-safe way

We caused a run-time type error, but we got advance warning at compile time

It can do this for lists too:

        fun Nth (0, (h::t)) = h
          | Nth (n, (h::t)) = Nth ((n-1), t) ;
                Warning: match nonexhaustive ...
                val Nth = fn : int * 'a list -> 'a


Next Copyright © 1999 M-J. Dominus