Files
Nim/tests/system/tvarargslen.nim
metagn 4ca2dcb404 Named arguments in commands + many grammar fixes (#20994)
* Breaking parser changes, implement https://github.com/nim-lang/RFCs/issues/442

Types are separated from expressions and better reflected in the grammar.

* add test

* more accurate grammar

* fix keyword typedescs

* accept expressions in proc argument lists

* CI "fixes"

* fixes

* allow full ref expressions again, adapt old tests

* cleanup, fix some tests

* improve grammar, try and revert semtypes change

* restrict sigil binding to identOrLiteral

* fix, should have caught this immediately

* add changelog entry, fix double not nil bug

* correct grammar

* change section

* fix

* real fix hopefully

* fix test

* support LL(1) for tuples

* make grammar.txt too
2022-12-06 13:11:56 +01:00

61 lines
1.4 KiB
Nim

discard """
output: '''
tvarargslen.nim:35:2 (1, 2)
tvarargslen.nim:36:2 12
tvarargslen.nim:37:2 1
tvarargslen.nim:38:8
done
'''
"""
## line 10
template myecho*(a: varargs[untyped]) =
## shows a useful debugging echo-like proc that is dependency-free (no dependency
## on macros.nim) so can be used in more contexts
const info = instantiationInfo(-1, false)
const loc = info.filename & ":" & $info.line & ":" & $info.column & " "
when varargsLen(a) > 0:
echo(loc, a)
else:
echo(loc)
template fun*(a: varargs[untyped]): untyped =
varargsLen(a)
template fun2*(a: varargs[typed]): untyped =
a.varargsLen
template fun3*(a: varargs[int]): untyped =
a.varargsLen
template fun4*(a: varargs[untyped]): untyped =
len(a)
proc main()=
myecho (1, 2)
myecho 1, 2
myecho 1
myecho()
doAssert fun() == 0
doAssert fun('a') == 1
doAssert fun("asdf", 1) == 2
doAssert fun2() == 0
doAssert fun2('a') == 1
doAssert fun2("asdf", 1) == 2
doAssert fun3() == 0
doAssert fun3(10) == 1
doAssert fun3(10, 11) == 2
## shows why `varargsLen` can't be named `len`
doAssert fun4("abcdef") == len("abcdef")
## workaround for BUG:D20191218T171447 whereby if testament expected output ends
## in space, testament strips it from expected output but not actual output,
## which leads to a mismatch when running test via megatest
echo "done"
main()