Shadowing infix pervasives


#1

This code:

module Px {
  let (+) = (`px(x), `px(y)) => `px(x+y);
}

let fourPx = Px.(`px(2) + `px(2));

begets a warning 44:

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

Is it because + is a pervasive and has a special treatment? If I replace it with, say, +++, the warning goes away.

More importantly, is the warning useful? And can I make it go away for usages like this (other than disabling the warning 44 altogether)?


#2

I think the warning in general is useful, but it’s an annoying developer experience hole in OCaml that the expression-level local open doesn’t provide any way to switch it off. You can switch it off in a block scope, e.g.

let fourPx = {
  open! Px;
  `px(2) + `px(2);
}

But in expression scope there’s no corresponding (e.g.) Px.!(...)

P.S. there’s nothing special about (+) or anything in Pervasives, btw. The same warning happens for any value shadowed by a module open.


#3

Yeah, block scope for a single operation is too much code. Even this looks better:

let fourPx = Px.(+)(`px(2), `px(2));

At least it’s a one-liner, even though it defies the purpose of an inline form.

Then again, when you have several lines of arithmetic like this, the block scope might help.
Sure, when you have more than 2 or 3 lines, shadowing becomes actually dangerous, so the window where open! is a good idea is rather narrow. But I believe that window exists, so thanks for the tip.