echo(1,2) != echo (1,2) ; where is this documented?
inim INim 0.2.0 Nim Compiler Version 0.18.1 [MacOSX: amd64] at /Users/timothee/.nimble/bin/nim >>> echo (1,2) (Field0: 1, Field1: 2) >>> echo(1,2) 12 >>> echo (1,) 1 >>> echo (1) 1
- what are the rules on whether we're passing a single tuple argument or multiple arguments?
- shouldn't echo (1,) be parsed as if we were passing a tuple of size 1, ie (Field0: 1) ?
T* he only place I could find is this: https://nim-lang.org/docs/manual.html#syntax-precedence > Whether an operator is used a prefix operator is also affected by preceding whitespace (this parsing change was introduced with version 0.13.0):
echo $foo # is parsed as echo($foo)
however this doesn't describe the story above
- https://github.com/nim-lang/Nim/wiki/Whitespace-FAQ also doesn't mention that
- NOTE: not limited to echo, this is also case for the way this would work: macro foo*(n: varargs[untyped]): typed = ...
- see also https://github.com/nim-lang/Nim/issues/7578 [RFC] let a=(1,) should return a tuple (int,) not an int
- special casing 1-element tuples to not be tuples breaks generic code, eg:
#nimble install tuples import tuples proc foo[T](a:T)=echo a.len foo(("abcdef", "ghi", "def")) # 3 foo(("abcdef", "ghi")) #2 foo(("abcdef", )) # 6 (oups!) instead of 1 foo(()) # 0
- same reason why fixed length arrays of size 1 exist, or named tuples of size 1 exist.
- allows concatenating with singleton tuples with no ambiguity, eg:
import tuples proc getCalculatorTestCases[Calculator]():auto = let a = (("1+1", 2), ("2*3", 6)) when Calculator.supportsDivision: return a & (("8 / 2", 4),) # without 1 element tuple, this would be: (("1+1", 2), ("2*3", 6), "8 / 2", 4) which is not what we want here else: return a
I agree when incomplete tuple creation should be an error instead.
It's just, tuple for me just a convenient way to return multiple values. Usually I used it in helper procs that those just defined to help the main proc.
So return tuple with only a field is kinda weird considering tuple is there to get multiple values.