# Unused variable vs let () and let \_?

**URL:** <https://discuss.ocaml.org/t/unused-variable-vs-let-and-let/9081>\
**Category:** Learning\
**Created:** [January 1, 2022, 8:31am UTC](https://discuss.ocaml.org/t/unused-variable-vs-let-and-let/9081 "2022-01-01T08:31:54Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![inariinari](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/inariinari/32/3435_2.png) [@inariinari](https://discuss.ocaml.org/u/inariinari)\
**Post date:** [January 1, 2022, 8:31am UTC](https://discuss.ocaml.org/t/unused-variable-vs-let-and-let/9081/1 "2022-01-01T08:31:54Z")

</div>

Let’s say I have a bunch of nested let…in statements. I noticed that if I have

```auto
(* a bunch of let... in above *)
let test = expr1 in
(* a bunch of let ... in below *)

```

I get this `Error (warning 26): unused variable test`, yet when I change it to `let _ = expr1 in ...` or `let () = expr1 in ...` there are no error messages.

I understand `let _ = ` is pattern matching for anything and `let () =` is pattern matching for anything of type unit, but does this mean that my `expr1` is just being executed anywhere? Is it being called once?

---

<div class="post-metadata">

**Author:** ![nojb](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/nojb/32/519_2.png) [@nojb](https://discuss.ocaml.org/u/nojb)\
**Post date:** [January 1, 2022, 9:26am UTC](https://discuss.ocaml.org/t/unused-variable-vs-let-and-let/9081/2 "2022-01-01T09:26:18Z")

</div>

> [@inariinari](#):
>
> does this mean that my `expr1` is just being executed anywhere? Is it being called once?

Yes, `expr1` is being evaluated, but the warning is telling you that its result, while being bound to a variable, is not being used.

As `expr1` is of type `unit` there is no point in binding its result to a variable. The usual way is to use the sequencing operator `;` in that case:

```auto
...
expr1;
...

```

If `expr1` _did_ return something other than `unit` then, indeed, `let _ = expr1` or `let _test = expr1` would be the way to tell the compiler that you want to explicitly ignore its return value. In general preceding a name with an underscore signals the compiler that it should not trigger an “unused” warning for that name.

Cheers,  
Nicolas

---

<div class="post-metadata">

**Author:** ![glennsl](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/glennsl/32/142_2.png) [@glennsl](https://discuss.ocaml.org/u/glennsl)\
**Post date:** [January 1, 2022, 1:04pm UTC](https://discuss.ocaml.org/t/unused-variable-vs-let-and-let/9081/3 "2022-01-01T13:04:11Z")

</div>

Cross-posted to [ocaml - Unused variable vs let () and let \_? - Stack Overflow](https://stackoverflow.com/questions/70547726/unused-variable-vs-let-and-let)
