Using `open` vs. warnings

This code:

let calculation = (x: float, y: float): float => {
  open Js.Math
  max_float(abs_float(x) -. abs_float(y), x +. y)
}

Generates the following warnings:

this open statement shadows the value identifier max_float (which is later used)
this open statement shadows the value identifier abs_float (which is later used)

How do I turn off these warnings? In a long program, they make open much less appealing if the increased readability of source is offset by having to check through dozens of warning messages.

Instead of using open, you can just write:

let calculation = (x: float, y: float): float => {
  Js.Math.max_float(abs_float(x) -. abs_float(y), x +. y)
}

A good solution for this instance. The real problem is that max_float and abs_float are also in Pervasives, which is what generates the message. ceil_float (which isn’t in Pervasives) works great with the local open and doesn’t generate any warning.

If shadowing is intentional you can always use open!

let calculation = (x: float, y: float): float => {
  open! Js.Math
  max_float(abs_float(x) -. abs_float(y), x +. y)
}

Adding a ! character after the open keyword indicates that shadowing is intentional and should not trigger the warning.

9 Likes