Skip to content

Commit

Permalink
updated readme, nim.cfg, nim version, and chained conditional access …
Browse files Browse the repository at this point in the history
…test
  • Loading branch information
Mat Taylor authored and Mat Taylor committed Jan 15, 2022
1 parent d90d2bd commit d498ec6
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 9 deletions.
2 changes: 1 addition & 1 deletion elvis.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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)))

Expand Down
2 changes: 1 addition & 1 deletion elvis.nimble
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
10 changes: 6 additions & 4 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
Binary file modified tests
Binary file not shown.
11 changes: 8 additions & 3 deletions tests.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)

#[
Expand Down

0 comments on commit d498ec6

Please sign in to comment.