Вопрос

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