How does one find out whether a field in an object in an array exists? The 
commented out `echo declared(c1.content[0].thing)` errors.

How do I find whether a value in a field of an object in an array exists using 
a proc, or template? The commented out code shows my failure.
    
    
    type
      Box = object
        thing: string
        food: bool
      
      Container = object
        content: seq[Box]
    
    var c1 = Container(
      content: @[
        Box(thing: "cake", food: true),
        Box(thing: "truck", food: false),
        Box(thing: "black puddin'", food: true),
        Box(thing: "dandelion", food: true)
      ]
    )
    
    echo type c1
    echo type c1.content
    echo type c1.content[0]
    echo type c1.content[0].thing
    echo declared(c1.content)
    #echo declared(c1.content[0].thing)
    
    var found = 0
    let item = false
    for obj in items(c1.content):
      if obj.food == item:
        echo found
      inc(found)
    
    #proc findInObjSeq*[T, S, P](a: T, field: P, item: S): int {.inline.} =
    #  result = 0
    #  for obj in items(a):
    #    if obj.field == item: return
    #    inc(result)
    #  result = -1
    #
    #echo findInObjSeq(c1, food, true)
    
    
    
    Run

Reply via email to