Search code examples
smlml

How to have more than one type in ML?


Is it possible in ML to allow a variable in ML to have more than one type?

For instance, If I want a node in a tree to only be an int or a string.

Node of int * string

I tried this but it just results in a tuple type of (int, string). I don't want it to be a tuple, just either an int or a string. Is this allowed?


Solution

  • As pointed out in a comment, the feature you're looking for is union types. I believe the syntax for them in SML is:

    datatype Node = IntNode of int
                  | StringNode of string