I wanted to share a pattern I've been playing with in some scrap code.
    
    
    type
      Person = object
        name:string
        age:int
    
    proc val(typ:typedesc[Person], name:string, age:int):Person =
      # Possibly complicated initialization logic.
      result.name = name
      result.age = age
    
    proc new(typ:typedesc[Person], name:string, age:int):ref Person =
      result.new()
      result[] = typ.val(name, age)
    
    proc main =
      let person_ref = Person.new("Joe", 30)
      let person_val = Person.val("Joe", 31)
      echo person_val
      echo person_ref[]
    
    when isMainModule:
      main()
    
    

As little as it matters I like the feel of _Person.new()_ and _Person.val()_.

Side question: does anyone have a pattern they like for sharing logic between 
the procs that build objects and their ref counterparts?

Reply via email to