If your string is a compile time constant you're code is from a quick glance 
only missing one thing. You just need to change the type of field to 
static[string] and remove the stringification (just field instead of $field).

Though if the string is not known at compile time this is not that easy. In 
languages where this is possible all objects are just tables (hash maps) in 
disguise, which isn't the case for Nim.

You can use something like this (which could also be generated using a macro: 
    
    
    proc assign[T](person: var Person, field: string, val: T) =
      when T is int:
        case field
        of "age": person.age = val
        else: raiseAssert("cannot assign int field " & field)
      elif T is string:
        case field
        of "name": person.name = val
        of "job": person.job = val
        else: raiseAssert("cannot assign string field " & field)
    
    
    Run

Otherwise what exactly do you need this for? There might be a another more 
idiomatic way for it.

Reply via email to