Any side effect of using the wildcard underscore in the let command (i.e., Let _ = ... in) in OCaml?

When using OCaml, I almost always use the underscore in let _ = exp, especially when the result is expnot important, but the calculation inside it. For instance:

let _ = print_endline "abc" in
...
let _ = a: =! a + 1 in
...
let _ = do_some_thing ... in

So, I'm just wondering if there is any side effect using it let _ = ...?

+4
source share
3 answers

- . let _ = , , . , :

let f a b = ...
let _ = f 3 4

f:

let f a b c = ...

let _ = f 3 4 - , , , . () ignore, :

let () = ignore (f 3 4)
let () = print_endline "abc"

let _ = ... .

+10

, let _ = . , .

+1

The goal letis to bind values ​​to identifiers. If you do only side effects, it’s better to wrap it in a block begin .. end. In your case:

begin
    print_endline "abc";
    a := !a + 1;
    do_some_thing ();
end
+1
source

All Articles