diff options
author | Jake Leahy <jake@leahy.dev> | 2023-02-01 20:00:10 +1100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-02-01 10:00:10 +0100 |
commit | 900fe8f501838077a5a1ef3d6793a1e901083bf8 (patch) | |
tree | a6736e3df0613a72a9e10d50e7827856891a0579 /lib/core/macrocache.nim | |
parent | ff8ab06720976f18d75257a22bf8e23873514c67 (diff) | |
download | Nim-900fe8f501838077a5a1ef3d6793a1e901083bf8.tar.gz |
Add `contains` to `std/macrocache` (#21304)
* Add test cases * Implement contains for CacheSeq * Implement contains for CacheTable * Fix implementation of hasKey * Remove contains for CacheSeq Fix runnable examples I was accidently using --doccmd:skip so I didn't spot the failure locally * Implement hasKey as a VM callback instead of magic * Implement suggestions from PR Co-Authored-By: ringabout <ringabout@users.noreply.github.com> * Update lib/core/macrocache.nim --------- Co-authored-by: ringabout <ringabout@users.noreply.github.com> Co-authored-by: ringabout <43030857+ringabout@users.noreply.github.com>
Diffstat (limited to 'lib/core/macrocache.nim')
-rw-r--r-- | lib/core/macrocache.nim | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/lib/core/macrocache.nim b/lib/core/macrocache.nim index d4b103793..ffd07abb1 100644 --- a/lib/core/macrocache.nim +++ b/lib/core/macrocache.nim @@ -181,6 +181,32 @@ proc `[]`*(t: CacheTable; key: string): NimNode {.magic: "NctGet".} = # get the NimNode back assert mcTable["toAdd"].kind == nnkStmtList +proc hasKey*(t: CacheTable; key: string): bool = + ## Returns true if `key` is in the table `t`. + ## + ## See also: + ## * [contains proc][contains(CacheTable, string)] for use with the `in` operator + runnableExamples: + import std/macros + const mcTable = CacheTable"hasKeyEx" + static: + assert not mcTable.hasKey("foo") + mcTable["foo"] = newEmptyNode() + # Will now be true since we inserted a value + assert mcTable.hasKey("foo") + discard "Implemented in vmops" + +proc contains*(t: CacheTable; key: string): bool {.inline.} = + ## Alias of [hasKey][hasKey(CacheTable, string)] for use with the `in` operator. + runnableExamples: + import std/macros + const mcTable = CacheTable"containsEx" + static: + mcTable["foo"] = newEmptyNode() + # Will be true since we gave it a value before + assert "foo" in mcTable + t.hasKey(key) + proc hasNext(t: CacheTable; iter: int): bool {.magic: "NctHasNext".} proc next(t: CacheTable; iter: int): (string, NimNode, int) {.magic: "NctNext".} |