Thanks for replying. I created a working PR before I saw your reply. I saw the
issue with precedence as well and mattaylor the maintainer added `.?` as an
alternative for `?.` for higher precedence. I changed the implementation to
`untyped` instead of using a `proc` type.
So this works:
template `.?`*(left, right: untyped): untyped =
if ?left: left.right
else:
var res: typeof(left.right)
res
Run
So this works:
type
Data = ref object
val: int
Obj = ref object
data: Data
var nilObj:Obj
var objNilData = Obj()
var obj = Obj()
obj.data = Data()
obj.data.val = 10
...
suite "conditional access (chained)":
test "nil check": check(nilObj.?data == nil)
test "falsy on ref": check(nilObj.?data.?val == 0)
test "falsy on ref": check(objNilData.?data.?val == 0)
test "truthy on ref": check(obj.?data.?val == 10)
Run