Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do some OCaml functions take () as a parameter?

Tags:

ocaml

Example in Unix module:

val environment : unit -> string array

Why not just:

val environment : string array

?

like image 653
qrest Avatar asked Aug 16 '10 15:08

qrest


1 Answers

Because it denotes a function that takes a value of type unit as its parameter. The unit type is only inhabited by the value "()". This is usually used to mean that the function is going to perform some kind of IO or induce a side-effect, and needs no input. The second type signature you provided is the signature for a value, not a function that can be applied. If some expression were bound to this name, that expression would be evaluated at the time the value binding takes place, not at the time it is referenced (as is the case with function application).

like image 50
Gian Avatar answered Oct 12 '22 00:10

Gian