diff --git a/elvis.nim b/elvis.nim index 1d3022e..23dfb5d 100644 --- a/elvis.nim +++ b/elvis.nim @@ -48,13 +48,14 @@ template `?=`*[T](l: T, r: T) = (if not(?l): l = r) template `=?`*[T](l: T, r: T) = (if ?r: l = r) template `?.`*[T,U](left: T, right: proc (x: T): U):U = - if ?left and ?right(left): left.right + if ?left: + let r = left.right + if ?r: r else: default(typeof(left.right)) else: default(typeof(left.right)) - # alternate syntax for conditional access to boost operator precendence (https://github.com/mattaylor/elvis/issues/3) template `.?`*(left, right: untyped): untyped = - if ?left: + if ?left: let r = left.right if ?r: r else: default(typeof(left.right)) else: default(typeof(left.right)) diff --git a/tests b/tests index 9b6f05b..c7f763a 100755 Binary files a/tests and b/tests differ diff --git a/tests.nim b/tests.nim index 1fa4298..3321fec 100644 --- a/tests.nim +++ b/tests.nim @@ -88,10 +88,11 @@ suite "conditional access (chained)": test "falsy on ref": check(objNilData.?data.?val == 0) test "truthy on ref": check(obj.?data.?val == 10) -suite "conditional mutable": - var seq = @["one"] +suite "conditional access mutable": + var seq = @["zero", "one"] test "truthy getter": check(seq.?pop == "one") - test "falsey getter": check(seq.?pop == "") + #test "chained truthy": check(seq.?pop.?len) == 4) # fails as pop is called twice + test "falsey getter": check(seq.?pop.?len == 0) #[ suite "conditional access with args":