rename lenTuple and lenVarargs (#13639)

* rename 'lenTuple' to 'tupleLen'

Rationale:
`lenTuple` is a tuple consisting of lengths (e.g. `(1, 5, 0)`),
`tupleLen` is a length of a tuple (e.g. `tupleLen((1, 5, 0) == 3`)

* rename 'lenVarargs' to 'varargsLen'

The same rationale as a previous commit. Consistency.
This commit is contained in:
Miran 2020-03-12 20:07:02 +01:00 • committed by GitHub
commit 14b2354b7d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 33 additions and 32 deletions

View file

@ -66,7 +66,7 @@
- Added `sugar.collect` that does comprehension for seq/set/table collections. - Added `sugar.collect` that does comprehension for seq/set/table collections.
- Added `sugar.capture` for capturing some local loop variables when creating a closure. - Added `sugar.capture` for capturing some local loop variables when creating a closure.
This is an enhanced version of `closureScope`. This is an enhanced version of `closureScope`.
- Added `typetraits.lenTuple` to get number of elements of a tuple/type tuple, - Added `typetraits.tupleLen` to get number of elements of a tuple/type tuple,
and `typetraits.get` to get the ith element of a type tuple. and `typetraits.get` to get the ith element of a type tuple.
- Added `typetraits.genericParams` to return a tuple of generic params from a generic instantiation - Added `typetraits.genericParams` to return a tuple of generic params from a generic instantiation
- Added `os.normalizePathEnd` for additional path sanitization. - Added `os.normalizePathEnd` for additional path sanitization.

View file

@ -187,7 +187,7 @@ proc evalTypeTrait(c: PContext; traitCall: PNode, operand: PType, context: PSym)
var operand = operand.skipTypes({tyGenericInst}) var operand = operand.skipTypes({tyGenericInst})
let cond = operand.kind == tyTuple and operand.n != nil let cond = operand.kind == tyTuple and operand.n != nil
result = newIntNodeT(toInt128(ord(cond)), traitCall, c.graph) result = newIntNodeT(toInt128(ord(cond)), traitCall, c.graph)
of "lenTuple": of "tupleLen":
var operand = operand.skipTypes({tyGenericInst}) var operand = operand.skipTypes({tyGenericInst})
assert operand.kind == tyTuple, $operand.kind assert operand.kind == tyTuple, $operand.kind
result = newIntNodeT(toInt128(operand.len), traitCall, c.graph) result = newIntNodeT(toInt128(operand.len), traitCall, c.graph)

View file

@ -72,13 +72,13 @@ proc distinctBase*(T: typedesc): typedesc {.magic: "TypeTrait".}
## compile time error otherwise ## compile time error otherwise
proc lenTuple*(T: typedesc[tuple]): int {.magic: "TypeTrait", since: (1, 1).} proc tupleLen*(T: typedesc[tuple]): int {.magic: "TypeTrait", since: (1, 1).}
## Return number of elements of `T` ## Return number of elements of `T`
since (1, 1): since (1, 1):
template lenTuple*(t: tuple): int = template tupleLen*(t: tuple): int =
## Return number of elements of `t` ## Return number of elements of `t`
lenTuple(type(t)) tupleLen(type(t))
since (1, 1): since (1, 1):
template get*(T: typedesc[tuple], i: static int): untyped = template get*(T: typedesc[tuple], i: static int): untyped =

View file

@ -2682,10 +2682,10 @@ when defined(nimV2):
import system/repr_v2 import system/repr_v2
export repr_v2 export repr_v2
macro lenVarargs*(x: varargs[untyped]): int {.since: (1, 1).} = macro varargsLen*(x: varargs[untyped]): int {.since: (1, 1).} =
## returns number of variadic arguments in `x` ## returns number of variadic arguments in `x`
proc lenVarargsImpl(x: NimNode): NimNode {.magic: "LengthOpenArray", noSideEffect.} proc varargsLenImpl(x: NimNode): NimNode {.magic: "LengthOpenArray", noSideEffect.}
lenVarargsImpl(x) varargsLenImpl(x)
when false: when false:
template eval*(blk: typed): typed = template eval*(blk: typed): typed =

View file

@ -92,40 +92,40 @@ block distinctBase:
doAssert($distinctBase(typeof(b2)) == "string") doAssert($distinctBase(typeof(b2)) == "string")
doAssert($distinctBase(typeof(c2)) == "int") doAssert($distinctBase(typeof(c2)) == "int")
block: # lenTuple block: # tupleLen
doAssert not compiles(lenTuple(int)) doAssert not compiles(tupleLen(int))
type type
MyTupleType = (int,float,string) MyTupleType = (int,float,string)
static: doAssert MyTupleType.lenTuple == 3 static: doAssert MyTupleType.tupleLen == 3
type type
MyGenericTuple[T] = (T,int,float) MyGenericTuple[T] = (T,int,float)
MyGenericAlias = MyGenericTuple[string] MyGenericAlias = MyGenericTuple[string]
static: doAssert MyGenericAlias.lenTuple == 3 static: doAssert MyGenericAlias.tupleLen == 3
type type
MyGenericTuple2[T,U] = (T,U,string) MyGenericTuple2[T,U] = (T,U,string)
MyGenericTuple2Alias[T] = MyGenericTuple2[T,int] MyGenericTuple2Alias[T] = MyGenericTuple2[T,int]
MyGenericTuple2Alias2 = MyGenericTuple2Alias[float] MyGenericTuple2Alias2 = MyGenericTuple2Alias[float]
static: doAssert MyGenericTuple2Alias2.lenTuple == 3 static: doAssert MyGenericTuple2Alias2.tupleLen == 3
static: doAssert (int, float).lenTuple == 2 static: doAssert (int, float).tupleLen == 2
static: doAssert (1, ).lenTuple == 1 static: doAssert (1, ).tupleLen == 1
static: doAssert ().lenTuple == 0 static: doAssert ().tupleLen == 0
let x = (1,2,) let x = (1,2,)
doAssert x.lenTuple == 2 doAssert x.tupleLen == 2
doAssert ().lenTuple == 0 doAssert ().tupleLen == 0
doAssert (1,).lenTuple == 1 doAssert (1,).tupleLen == 1
doAssert (int,).lenTuple == 1 doAssert (int,).tupleLen == 1
doAssert type(x).lenTuple == 2 doAssert type(x).tupleLen == 2
doAssert type(x).default.lenTuple == 2 doAssert type(x).default.tupleLen == 2
type T1 = (int,float) type T1 = (int,float)
type T2 = T1 type T2 = T1
doAssert T2.lenTuple == 2 doAssert T2.tupleLen == 2
block genericParams: block genericParams:
type Foo[T1, T2]=object type Foo[T1, T2]=object

View file

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