summary refs log tree commit diff stats
path: root/lib/core/macrocache.nim
diff options
context:
space:
mode:
authorJake Leahy <jake@leahy.dev>2023-06-10 22:43:32 +1000
committerGitHub <noreply@github.com>2023-06-10 14:43:32 +0200
commitd3af0882cff6d1d4cbaf669005f6463d72e5be27 (patch)
tree426ea87725d1d96a9713b7a707875ce06c27a4d7 /lib/core/macrocache.nim
parentb2d77619757c73a8b14a8ccf127eaf3e630159cf (diff)
downloadNim-d3af0882cff6d1d4cbaf669005f6463d72e5be27.tar.gz
`BackwardsIndex` overload for `CacheSeq.[]` (#22043)
* Add `BackwardsIndex` support for `CacheSeq`

* Add changelog entry

---------

Co-authored-by: Andreas Rumpf <rumpf_a@web.de>
Diffstat (limited to 'lib/core/macrocache.nim')
-rw-r--r--lib/core/macrocache.nim13
1 files changed, 13 insertions, 0 deletions
diff --git a/lib/core/macrocache.nim b/lib/core/macrocache.nim
index ffd07abb1..39999fa11 100644
--- a/lib/core/macrocache.nim
+++ b/lib/core/macrocache.nim
@@ -127,6 +127,19 @@ proc `[]`*(s: CacheSeq; i: int): NimNode {.magic: "NcsAt".} =
       mySeq.add(newLit(42))
       assert mySeq[0].intVal == 42
 
+proc `[]`*(s: CacheSeq; i: BackwardsIndex): NimNode =
+  ## Returns the `i`th last value from `s`.
+  runnableExamples:
+    import std/macros
+
+    const mySeq = CacheSeq"backTest"
+    static:
+      mySeq &= newLit(42)
+      mySeq &= newLit(7)
+      assert mySeq[^1].intVal == 7  # Last item
+      assert mySeq[^2].intVal == 42 # Second last item
+  s[s.len - int(i)]
+
 iterator items*(s: CacheSeq): NimNode =
   ## Iterates over each item in `s`.
   runnableExamples: