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
- How can I implement a conditional access operator? geekrelief
- How can I implement a conditional access operator? planetis
- How can I implement a conditional access operator? Hlaaftana
- How can I implement a conditional access operator... geekrelief
- How can I implement a conditional access oper... geekrelief
- How can I implement a conditional access... geekrelief
- How can I implement a conditional ac... ElegantBeef
- How can I implement a conditiona... geekrelief
- How can I implement a conditiona... geekrelief
- How can I implement a conditiona... geekrelief
- How can I implement a conditiona... PMunch
- How can I implement a conditiona... rishab
- How can I implement a conditiona... geekrelief
