2
votes

I am following the wikibook on OCaml and they say that the integer type could have been constructed like that:

type int = 0 | 1 | 2 | (* .... *) | -1 | -2 | (* ... *) ;;

Later on, the book indicates that all constructors are words starting with an uppercase. There are also special constructors, true false and integers like 1, 2, 3.

So, back to utop. I type the following:

type d = 0 | 1 | 2 ;;

And I get a syntax error on the 0. What am I missing? Isn't 0 a constructor?

1

1 Answers

2
votes

As you say, it's not literally true that you could define the integers as variant type. Constructors have to be identifiers starting with a capital letter. But it's a way of thinking about constructors (and ints) that is enlightening in some ways. That's all.

By the way, true and false are really similar to constructors in the same sense as the ints. They don't start with a capital letter either. But it's enlightening to think of them as constructors.