diff --git a/elvis.nim b/elvis.nim index 23dfb5d..6814f99 100644 --- a/elvis.nim +++ b/elvis.nim @@ -60,7 +60,7 @@ template `.?`*(left, right: untyped): untyped = if ?r: r else: default(typeof(left.right)) else: default(typeof(left.right)) -#template `.?`*[T,U,V](left: T, right: proc (x: T, y:V):U, arg: V):U = +#template `.?`*[T,V,U](left: T, right: proc (x: T, y:V):U, arg: V):U = # if ?left: left.right(arg) # else: default(typeof(left.right(arg))) diff --git a/elvis.nimble b/elvis.nimble index c864f52..8586361 100644 --- a/elvis.nimble +++ b/elvis.nimble @@ -1,5 +1,5 @@ # Package -version = "0.2.0" +version = "0.3.0" author = "Mat Taylor" description = "truthy, elvis, ternary and conditional assignment operators for nim" license = "MIT" diff --git a/readme.md b/readme.md index 4621e02..155efea 100644 --- a/readme.md +++ b/readme.md @@ -91,12 +91,14 @@ assert (s == "a") ### Conditional Access Operator `.?` -The Conditional access operator will call the right operand with the left operand as thefirst argument when the left operand evaluates as truthy. Otherwise it will return a new unintiated instance (falsy) whatever type the right operand proc would have returned. +The Conditional access operator will call the right operand with the left operand as thefirst argument when the left operand evaluates as truthy. Otherwise it will return a new unintiated instance (falsy) whatever type the right operand proc would have returned. Chained conditional access is also supported for pertties and simple functions, however conidtioanl access to chained function calls with additonal arguments will currnetly not compile. __Examples:__ ```nim - let s = @["one"] - assert((s[0].?len) == 3) - assert((s[1].?len) == 0) + let opt0 = none(string) + let opt1 = some("one") + assert(opt0.?get.?len == 0) + assert(opt1.?get.?len == 3) + #assert(opt1.?get("none") == "none" # compile error ``` diff --git a/tests b/tests index c7f763a..93d43c0 100755 Binary files a/tests and b/tests differ diff --git a/tests.nim b/tests.nim index 3321fec..0ff6802 100644 --- a/tests.nim +++ b/tests.nim @@ -16,6 +16,8 @@ let str1 = "" let seq1 = @["one"] let tab0 = { "one": "uno" }.toTable let tab1 = { "one": 1 }.newTable +let opt0 = none(string) +let opt1 = some("one") type @@ -87,11 +89,14 @@ suite "conditional access (chained)": 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) + test "truthy chained proc": check(opt1.?get.?len == 3) + test "falsey chained proc": check(opt0.?get.?len == 0) + suite "conditional access mutable": - var seq = @["zero", "one"] - test "truthy getter": check(seq.?pop == "one") - #test "chained truthy": check(seq.?pop.?len) == 4) # fails as pop is called twice + var seq = @["one", "none"] + test "truthy getter": check(seq.?pop == "none") + #test "chained truthy": check(seq.?pop.?len) == 3) # chains fail as pop is called twice test "falsey getter": check(seq.?pop.?len == 0) #[