Warning 10: this expression must be of type unit

I am trying to create a list of functions in Ocaml, but I keep getting a warning. Any idea why?

let f = [fun x β†’ -x; fun x β†’ x + 2, fun x β†’ x * x]

+7
source share
1 answer

A half-column is also used to complete functions that are used for their side effects. A warning occurs when the return type of these functions is not equal to unit (in this case, int ); these are just warnings, as you can use side effects, this is usually a mistake. This is aloof, but to prevent these warnings programmatically and safely, use the ignore function, as in ignore (x+2); .

Let's get back to your problem, in it (and we will expand the half-columns to their equivalence and changing the variables for each function) that you actually write,

 (fun x -> let _ = -x in (fun y -> let _ = y+2 in (fun z -> z*z))) 

Or, another example, as Gasche points out,

 (fun x -> -x; (fun y -> y+2; (fun z -> z*z))) 

You can specify from the return type (int -> int -> int -> int) list that something does not instantly match your intentions. You will need to add parentheses around, for example (fun x -> x+2); to create a list.

+9
source

All Articles