I found the following behavior puzzling:
echo(1+1) # prints 2 as expectted
echo(1 +1) # Error: in expression '1 1': identifier expected, but found '1'
Notice a space between 1 and + in the second line. That makes all of the difference. Apparently, one can write 1 + 1 with spaces on both sides of +, or 1+1 with no spaces; but 1+ 1 or 1 +1 result in error. Weird...
https://play.nim-lang.org/#ix=2z9G
That was why we removed the unary < operator when people started to have bugs in their 1 .. <10 expression.
Also note that whitespace will change precedence of 1 + 2 * 2 vs 1+2 * 2
Also note that whitespace will change precedence of 1 + 2 * 2 vs 1+2 * 2
No, it is not:
echo (1 + 2 * 2) == (1+2 * 2) # returns true
https://play.nim-lang.org/#ix=2zae
but 1+ 1 or 1 +1 result in error. Weird...
Believe it or not, I consider this to be a nice feature. First of all, it means that the call syntax via juxtaposition (also used in eg. echo "hello world") is not compromised too much. Secondly, it prevents ugly code like yours that couldn't get the basic spacing around a binary operator right -- I don't want to read such code.
This is true for something like (foo +1). But (1 +1) cannot be interpreted as a function call. In any case, I am not criticizing Nim's design decisions and I like the fact that it is different from other languages in a good way. But when it is "unexpectedly different" one needs to properly document it and provide some relevant examples. In this case, the documentation refers to $ which is unary-only operator. While +- can be both unary and binary. Most of the time my code is "whitespace consistent" and I use either 1+1 or 1 + 1 when I type. But this time, I did a search/replace in vim and my search pattern was "eating up" whitespace. Compiler error:
Error: in expression '1 1': identifier expected, but found '1'
was not obvious to interpret for someone who sees it for the first time :-)
But (1 +1) cannot be interpreted as a function call.
It will still be parsed like that because it might be useful in a macro. Parsing it differently would introduce a syntactic inconsistency.