moonscript icon indicating copy to clipboard operation
moonscript copied to clipboard

Range comparison

Open aleclarson opened this issue 7 years ago • 7 comments

if 1 < x < 2
  print '`x` is between 1 and 2 (non-inclusive)'

..would compile to:

if x > 1 and x < 2 then
  print('`x` is between 1 and 2 (non-inclusive)')
end

aleclarson avatar Feb 22 '18 15:02 aleclarson

This is definitely something I miss from Python; however, I do think it should compile to if 1 < x and x < 2 - definitely a nitpick, but I think it'd help with debugging/looking at compiled code.

RyanSquared avatar Feb 22 '18 15:02 RyanSquared

Definitely what @RyanSquared said, it also prevents surprised with overloaded operators. Should cache the middle one as well, in case it has side-effects.

vendethiel avatar Feb 22 '18 15:02 vendethiel

Right so it'd compile to local _comparison_0 = x ; if 1 < _comparison_0 and _comparison_0 < 2.

RyanSquared avatar Feb 22 '18 15:02 RyanSquared

The local declaration is only necessary if doing x.y or x!, or could x really have a side effect?

aleclarson avatar Feb 22 '18 15:02 aleclarson

It actually is necessary. Example:

setmetatable(_ENV, {__index = coroutine.wrap(function()
  local n = 1
  while true do
    coroutine.yield(n)
    n = n + 1
  end
end)})
print(a, a, a)

Now, if you do this yourself, why. However, it still is a side effect.

RyanSquared avatar Feb 22 '18 15:02 RyanSquared

Touché.
This is a nitpick, but maybe _cmp_0 would be a more concise (yet just as clear?) name for the compiled result. ¯\(ツ)

aleclarson avatar Feb 22 '18 16:02 aleclarson

(side note: I've never seen such a snarky looking shrug)

And yeah, that was just a quick example; I'd be fine with _cmp_0 given how popular "cmp" is as a shortening of the word, not that it should matter much outside of debugging anyways.

RyanSquared avatar Feb 22 '18 16:02 RyanSquared