Ignore function in OCaml elegantly

I define the f function in utop version of OCAMl 4.04.

utop # let f = function x -> x + 1;;

val f : int -> int = <fun> 

When I try to ignore f, I come across a warning.

utop # let a = ignore (f : int -> int); f 2;;

Characters 15-19:                                             
Warning 5: this function application is partial,
maybe some arguments are missing
val a : int = 3

Warning 5 is triggered because the expression following ignorehas a function interface int -> int.

ignore (f 0)and they if false then (ignore f 0)work, but they are not elegant. I do not want to provide missing arguments f. Is there an alternative ignore?

The motivation is ignorenot very clear in this example, but I need to use it to avoid other warnings in my real project.

Thank you for your time.

+4
source share
4 answers

Roughly speaking, an e1; e2OCaml typechecker is processed to express a form as follows:

  • e1 t -> t', 5.
  • else, e1 unit, 10.

,

let f x = 
    prerr_endline "some side effect you may want"; 
    (* but you may not want the returned function sometimes *)
    fun y -> x + y

let a = f 1 2; f 1 2;;       (* Warning 10 *)
let a = f 1; f 1 2;;         (* Warning 5 *)

ignore e1; e2 e1; e2 : : 10 , e1 unit. 5 :

let a = ignore (f 1 2); f 1 2;;  (* No warning *)
let a = ignore (f 1); f 1 2;;    (* Warning 5 *)

So ignore 10, 5. ignore is_ignore funciton typing/typecore.ml OCaml.

5, 2 :

let a = let _ = f 1 in f 1 2;;   (* No warning *)

let ignore' _ = ()
let a = ignore' (f 1); f 1 2;;   (* No warning *)

- wild card. - ignore'. OCaml-, ignore, 5 , .

+4

, ignore let _:

let a = let _ = (f : int -> int) in f 2;;
+1

, , :

let ignore_fun (f:'a -> 'b) = ()

, f, .

let a = ignore_fun (f : int -> int); f 2;;
0

camlspotter , . :

let a = ignore (f,()); f 2;;

let a = ignore [f]; f 2;;

-, .

0

Source: https://habr.com/ru/post/1683671/


All Articles