tags:

views:

85

answers:

1

Example in Unix module:

val environment : unit -> string array

Why not just:

val environment : string array

?

+10  A: 

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).

Gian
So the difference is that, by having a function take *some* parameter, you force evaluation to be done when it is referenced, hence getting the IO to be effected at the time intended?
qrest
Precisely! Unit just provides a convenient value to act as a parameter. For this reason it is often use in this context, because it's a value but doesn't really represent anything meaningful. I kinda used weasel words when I said "referenced". I should have said "applied" (functions are values, and therefore names with functions bound to them can be referenced without being applied).
Gian