forked from nim-lang/Nim
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
lenVarargs: number of varargs elements
- Loading branch information
1 parent
e5ed4c1
commit 45dbc65
Showing
2 changed files
with
61 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
discard """ | ||
output: ''' | ||
tlenvarargs.nim:35:9 (1, 2) | ||
tlenvarargs.nim:36:9 12 | ||
tlenvarargs.nim:37:9 1 | ||
tlenvarargs.nim:38:8''' | ||
""" | ||
|
||
|
||
## 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 lenVarargs(a) > 0: | ||
echo(loc, a) | ||
else: | ||
echo(loc) | ||
|
||
template fun*(a: varargs[untyped]): untyped = | ||
lenVarargs(a) | ||
|
||
template fun2*(a: varargs[typed]): untyped = | ||
a.lenVarargs | ||
|
||
template fun3*(a: varargs[int]): untyped = | ||
a.lenVarargs | ||
|
||
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 `lenVarargs` can't be named `len` | ||
doAssert fun4("abcdef") == len("abcdef") | ||
|
||
main() |