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

  • 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
2018-04-12 00:37:21
What is the use of tuple with only one field? 2018-04-12 05:02:22
@mashingan If any-length tuple is expected, a tuple with one or zeros elements is the right thing to use. It works really well with generic code on tuples. 2018-04-12 08:19:05
  • 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.

EDIT

  • 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

2018-04-12 08:44:56

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.

2018-04-12 09:57:50
hope the example i gave above was able to convince you. In any case (1,) is now supported.
2018-04-16 00:42:40

@mashingan

It is not. It's a common usecase, not the definition. You can use integers to denote count but that's not the definition of an integer.

2018-04-16 12:27:13