I'm a beginner in OCaml, and i have a problem with series of let, like:

let x = myFun y in
let z = myFun x in
...

But, between the lets, i use Hashtbl.iter and other functions that returns unit. If i do:

let x = myFun y in
Hashtbl.iter (...) ...
let z = myFun x in
...

The compiler raises an error. But, if i do:

let x = myFun y in
let h = Hashtbl.iter (...) ...
let z = myFun x in
...

The compiler don't raise any error, just warnings showing that "h" is never used. What's the best way to do that without any problem? Thanks.

有帮助吗?

解决方案

You have to use ; to separate expressions:

e1 ; e2

This performs the side-effects in e1 and discards the result, and then evaluates `e2.

So in your case:

let x = myFun y in
Hashtbl.iter (...) ... ;
let z = myFun x in
...

其他提示

Note that you can write

let x = ... in
  let _ = Hashtbl.iter in ...
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top