You could write this as a macro, but it's also possible as a template and it's 
clearer this way. Template substitution works for field access, which makes 
this possible.
    
    
    template `?.`(a, b): untyped =
      let tmp = `a` # ensure `a` isn't evaluated twice
      if tmp.isNil:
        nil
      else:
        tmp.`b`
    
    type Prop = object
      val*: int
    
    type Data = object
      prop*: ptr Prop
    
    var data: ptr Data = cast[ptr Data](alloc(sizeof(Data)))
    
    data?.prop
    
    
    Run

Reply via email to