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?