echo(not 0 >= 5)
Will output false because not 0 is evaluated first as a bitwise not. @Araq mentioned on IRC the possibility of using ~ instead.
@dom96, can you please point me to the relevant code snippet responsible for this? not is quite liberally used in the codebase.
@mrordinaire hrm? i'm not sure what you mean, the code snippet is right there.
@dom96 I don't know what's the objective for this issue is. grepping for the code snippet does not turn up anything.
The code snippet is just an example of a gotcha.
echo(not(0 >= 5))
would work right?
if so, I don't see this as a deficiency in the language. Many languages will evaluate the following to false
! false == false
and would require parentheses to evaluate true
!(false == false)
For the record, Python evaluates the test as True. The people I've heard talk about Nim are Python programmers, so at least some part of Nim's audience may expect a Python-leaning result.
>>> not 0 >= 5
True
The difference between Python and Nim here is that Nim's not is overloaded for ints, whereas Python uses ~ as a bitwise not.
I'm assuming that @Araq would like to see ~ used for Nim as well, and for not to work only for booleans.
@dom96 @Araq I would like to work on this. How do I approach this issue?
Patch system.nim so that not for integers is .deprecated and introduce proc bnot*() that maps to the underlying magics like the then .deprecated not operators.
@Araq Why proc bnot*(), shouldn't it be proc `~`*()
~ is too useful as an user definable operator but it's debatable, if it's only defined for integers I can still overload it.
I would vote for a compiler warning that suggests to put parenthesis. This is done in some C compilers as well and works well. On the other hand, who writes that example?
echo(not(0 >= 5)) # should be echo(0 < 5)
echo(not a == b) # should be echo(a != b)
I think that was @Araq's plan but it turned out to be tough to implement reliably.
Interesting though. AFAIK nim already has term rewriting macros. Shouldn't this be a simple term rewriting macro that emits a warning and does nothing otherwise?
Warning about this is unresonably hard to implement. I think we can live with a wart that comes up once in a decade.
Most helpful comment
I would vote for a compiler warning that suggests to put parenthesis. This is done in some C compilers as well and works well. On the other hand, who writes that example?