bugfix: 'indexOf' for tuple fields works
This commit is contained in:
parent
5ac5bedc66
commit
78b27ed7fa
3 changed files with 30 additions and 7 deletions
21
tests/run/tfieldindex.nim
Normal file
21
tests/run/tfieldindex.nim
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
discard """
|
||||
output: "1"
|
||||
"""
|
||||
|
||||
type
|
||||
TMyTuple = tuple[a, b: int]
|
||||
|
||||
proc indexOf*(t: typedesc, name: string): int {.compiletime.} =
|
||||
## takes a tuple and looks for the field by name.
|
||||
## returs index of that field.
|
||||
var
|
||||
d: t
|
||||
i = 0
|
||||
for n, x in fieldPairs(d):
|
||||
if n == name: return i
|
||||
i.inc
|
||||
raise newException(EInvalidValue, "No field " & name & " in type " &
|
||||
astToStr(t))
|
||||
|
||||
echo TMyTuple.indexOf("b")
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue