mirror of
https://github.com/logos-storage/questionable.git
synced 2026-01-02 13:53:11 +00:00
Indexing of strings and sequences should not catch Defect
Catching a Defect does not always work, depending on how it's compiled.
This commit is contained in:
parent
416b6dd566
commit
e56cf86c4a
@ -1,7 +1,16 @@
|
||||
import std/macros
|
||||
|
||||
when (NimMajor, NimMinor) < (1, 4):
|
||||
type IndexDefect = IndexError
|
||||
macro `.?`*(expression: seq | string, brackets: untyped{nkBracket}): untyped =
|
||||
# chain is of shape: (seq or string).?[index]
|
||||
let index = brackets[0]
|
||||
quote do:
|
||||
block:
|
||||
type T = typeof(`expression`[`index`])
|
||||
let evaluated = `expression`
|
||||
if `index` < evaluated.len:
|
||||
evaluated[`index`].some
|
||||
else:
|
||||
T.none
|
||||
|
||||
macro `.?`*(expression: typed, brackets: untyped{nkBracket}): untyped =
|
||||
# chain is of shape: expression.?[index]
|
||||
@ -13,5 +22,3 @@ macro `.?`*(expression: typed, brackets: untyped{nkBracket}): untyped =
|
||||
`expression`[`index`].some
|
||||
except KeyError:
|
||||
T.none
|
||||
except IndexDefect:
|
||||
T.none
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user