OCaml - What data type is some and none?

ocaml, types

Solution

What you have written here is a value of type `(int option * int option * 'a option * int option) list`, i.e. a list of quadruplets in which the first two component are optional integer, the next one is polymorphic so far (the Some case is undefined yet) and the last one is an optional integer. This is because `,` is the tuple separator. The list separator is `;` so I guess you wanted to write

let listVar = [Some 4; Some 3; None; Some 2];;

Which has type `(int option) list`. Whenever you use `Some` or `None` with an arbitrary value of type `'a` you get a value of type `'a option`.

Problem

If I am using `Some` and `None` combo in a list what would be the data type of the list? Is it always `'a`? Or is there some sort of a type for `Some`/`None`? ``` let listVar : (* type here *) list = [Some 4; Some 3; None; Some 2];; ``` If I put `int` it gives me error: This expression has type int option * int option * 'a option * int option but is here used with type int When I put `'a` it compiles fine but the basic OCaml tutorial says (I made references to other languages to explain my question better): It won't be clear yet why polymorphic functions are useful, but they are very useful and very common, and so we'll discuss them later on. (Hint: polymorphism is kind of like templates in C++ or generics in Java 1.5). I thought this was like reference/pointer in other languages, which actually made sense. But now, I really don't understand what type is `None`. Same goes with `Some`. Also, I know I should ask two questions in one question, but this one has a strong relation to the previous question. What is the point of Some? I usually see it used when None is used. If I implement the above list without Some, it still compiles but the list structure doesn't have the "option" flag, which I am guessing means optional (I cant seem to find anything on the internet regarding this). Can someone provide me with one case this is useful?

Original source