Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does "let () = " mean in Ocaml?

Tags:

let

ocaml

There are codes like

let () = print_string "something" in
fn

in some OCaml codes.

What does this mean? Is there special meaning on "()"? or Is it same meaning as

print_string "something";
fn
like image 683
Doe Avatar asked Sep 23 '11 05:09

Doe


People also ask

What does :: do in OCaml?

It is usually used if you have a function or value that is very similar to some other, but is in some way new or modified. Regarding the :: symbol - as already mentioned, it is used to create lists from a single element and a list ( 1::[2;3] creates a list [1;2;3] ).

What does the operator do in OCaml?

Comparisons (Relational Operators) OCaml distinguishes between structural equality and physical equality (essentially equality of the address of an object). = is structural equality and == is physical equality. Beware: <> is structural not-equals while != is physical not-equals.

How do I return a value in OCaml?

OCaml doesn't have a return keyword — the last expression in a function becomes the result of the function automatically.

What does arrow mean in OCaml?

The operator is usually called type arrow where T1 -> T2 represents functions from type T1 to type T2 . For instance, the type of + is int -> (int -> int) because it takes two integers and returns another one.


2 Answers

There's nothing special about () in this let expression, it's just a pattern. All let expressions look like let pattern = expression in other-expression. Here the pattern will always match, because print_string returns unit, and () is the only value of that type. In this way, it's just another way of combining two expressions into one when the first one is really more of a statement (returns unit).

So you're right, the construct has pretty much the same meaning as using the ; operator. The only real difference is in the precedence. If, for example, you write

if x < 3 then
    print_string "something";
    f x

you would find that f x is always called. The precedence of ; is too low to pull the second expression under the control of the if. That's the reason many people (including me) get into the habit of using let () = expression. If you write the above as

if x < 3 then
    let () = print_string "something"
    in f x

the f x is only called when x is less than 3, which is usually what I want. In essence, the precedence of let is much higher than ;.

Of course there are may other ways to get this effect, but the nice thing about using let is that you don't have to add anything later on in the code (like a closing parenthesis or an end). If you're adding the print_string as a debugging statement, this is a handy way of keeping the changes local to the one spot.

like image 68
Jeffrey Scofield Avatar answered Nov 24 '22 09:11

Jeffrey Scofield


Jeffrey's answer is absolutely correct, but one more point:

if you write

fx "something";
fn

And you messed up the result type of fx "something" the compiler will emit a Warning, that might get lost during compilation. On the other hand if you write:

let () = fx "something" in
fn

The compiler will type check that the result of fx "something" can be matched against (), i.e. that it is really of type unit. Thus if you messed up an error is produced, which is usually more secure.

There also is the possibility to write

let _ = fx "something" in
fn

which will only get the precedence effect that Jeffrey mentioned, but not do any type checking, since _ can be matched against values of any type.

like image 45
LiKao Avatar answered Nov 24 '22 08:11

LiKao