mirror of
https://github.com/logos-storage/questionable.git
synced 2026-01-05 23:33:12 +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
78ed49ce86
@ -1,7 +1,16 @@
|
|||||||
import std/macros
|
import std/macros
|
||||||
|
|
||||||
when (NimMajor, NimMinor) < (1, 4):
|
macro `.?`*(expression: seq | string, brackets: untyped{nkBracket}): untyped =
|
||||||
type IndexDefect = IndexError
|
# 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 =
|
macro `.?`*(expression: typed, brackets: untyped{nkBracket}): untyped =
|
||||||
# chain is of shape: expression.?[index]
|
# chain is of shape: expression.?[index]
|
||||||
@ -13,5 +22,3 @@ macro `.?`*(expression: typed, brackets: untyped{nkBracket}): untyped =
|
|||||||
`expression`[`index`].some
|
`expression`[`index`].some
|
||||||
except KeyError:
|
except KeyError:
|
||||||
T.none
|
T.none
|
||||||
except IndexDefect:
|
|
||||||
T.none
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user