September 22, 1999 Strong Typing Slide #21

Union Type Sabotage

        datatype MyNum = IV of int | FV of real;
        val n = IV(3);
                val n = IV 3 : MyNum

Let's try again to screw up the union types:

        fun intval (IV i) = i
          | intval (FV f) = f ;
                Error: rules don't agree (tycon mismatch)
                  expected: MyNum -> int
                  found:    MyNum -> real
                    FV x => x

One last try:

                Error: operator and operand don't agree (tycon mismatch)
                  operator domain: real
                  operand:         MyNum
                  in expression:
                    sqrt (n)

Next Copyright © 1999 M-J. Dominus