summary refs log blame commit diff stats
path: root/tests/fields/tfieldindex.nim
blob: 6de6d54bdbc6e9ba8cbe01e01dd4c7b3d3b8edb4 (plain) (tree)
1
2
3
4
5
6
7
8






                             
                                               







                                                   
                                                                   



                          
discard """
  output: "1"
"""

type
  TMyTuple = tuple[a, b: int]

proc indexOf*(t: typedesc, name: string): int =
  ## 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(ValueError, "No field " & name & " in type " &
    astToStr(t))

echo TMyTuple.indexOf("b")