Indeed, there is an ignore : 'a -> unit
function that does exactly this.
The compiler actually knows about ignore
and hard-code a specific behavior that is generally very useful, but can occasionally be inconvenient: by default, ignoring a function (anything with a type of the form foo -> bar
) will raise a warning.
The reason for this is that forgetting to add the last argument of a function is a relatively common mistake. You may write for example
ignore (List.map (fun x -> foo bar))
while you meant to write
ignore (List.map (fun x -> foo bar) li)
and the effects you expect to see applied are not.
So ignore
will raise a warning in this case, which your hand-coded let ignore x = ()
does not do. People also sometimes write
let _ = foo in ...
to ignore foo
, but this suffer from the same downside (it's easy to be wrong about what the type actually is). Kakadu recommends to use a type annotation there, and that's a reasonable advice.
(Some people also use let _ = ...
for the "main" expression of their program. I recommend to rather use let () = ...
for this same reason that it forces more type precision.)
Finally, some people prefer to use the let _ = ...
form because they are in situations where using ;
would force them to add parentheses or begin..end
. I recommend that you always add parentheses or begin..end
in these cases, because it's easy to shoot yourself in the foot with that -- obscure precedence rules for if..then..else..
for example.
ignore
method is in language, as you defined it. – Raynor