echo len"a" + 1 # Ok, parses as len("a") + 1
echo ord'a' + 1 # Error, parses as ord('a' + 1)
I think the second line must be parsed as the first oneSorry, I don't know how to change the wrong tittle.
In any case, what's the rationale for this different "parsing precedence"
import macros
dumpTree:
len"a" + 1
ord'a' + 1
ord 'a' + 1
output:
StmtList
Infix
Ident "+"
CallStrLit
Ident "len"
RStrLit "a"
IntLit 1
Command
Ident "ord"
Infix
Ident "+"
CharLit 97
IntLit 1
Command
Ident "ord"
Infix
Ident "+"
CharLit 97
IntLit 1
len"a" is a Generalized raw string literals. 'a' is a character literal but there is no generalized character literals. https://nim-lang.github.io/Nim/manual.html#lexical-analysis-generalized-raw-string-literals
So ord'a' + 1 is parsed as Command invocation syntax.
Perfect explanation, thank you!
It's a pity that I can not save myself a couple of parenthesis writing ord'p' + 2. I hate unnecessary parentheses and that's why (among other things) I love the NIM language
This was just an example. There are other examples where your notation is not possible and the parentheses are mandatory:
let someBool = true
if "ask".startsWith'a' and someBool:
# Error, parsed as startsWith('a' and someBool)
echo "...."
I think that for consistency with the string case this could be parsed as startsWith('a') and someBool