mirror of
https://github.com/logos-storage/questionable.git
synced 2026-03-31 16:53:27 +00:00
Fix for Nim 1.2.x
This commit is contained in:
parent
48e8fe845f
commit
a884e5bf64
@ -1,29 +1,24 @@
|
|||||||
import std/macros
|
import std/macros
|
||||||
|
import std/options
|
||||||
|
|
||||||
macro `.?`*(expression: seq | string, brackets: untyped{nkBracket}): untyped =
|
proc safeGet[T](expression: seq[T] | openArray[T], index: int): Option[T] =
|
||||||
# chain is of shape: (seq or string).?[index]
|
if index >= expression.low and index <= expression.high:
|
||||||
let index = brackets[0]
|
expression[index].some
|
||||||
quote do:
|
else:
|
||||||
block:
|
T.none
|
||||||
type T = typeof(`expression`[`index`])
|
|
||||||
let evaluated = `expression`
|
|
||||||
if `index` >= evaluated.low and `index` <= evaluated.high:
|
|
||||||
evaluated[`index`].some
|
|
||||||
else:
|
|
||||||
T.none
|
|
||||||
|
|
||||||
macro `.?`*(expression: openArray, brackets: untyped{nkBracket}): untyped =
|
proc safeGet(expression: string, index: int): Option[char] =
|
||||||
# chain is of shape: openArray.?[index]
|
if index >= expression.low and index <= expression.high:
|
||||||
let index = brackets[0]
|
expression[index].some
|
||||||
quote do:
|
else:
|
||||||
block:
|
char.none
|
||||||
type T = typeof(`expression`[`index`])
|
|
||||||
proc safeGet(arr: openArray[T], i: int): ?T {.gensym.} =
|
macro `.?`*(expression: seq | string | openArray, brackets: untyped{nkBracket}): untyped =
|
||||||
if i >= arr.low and i <= arr.high:
|
# chain is of shape: (seq or string or openArray).?[index]
|
||||||
arr[i].some
|
let index = brackets[0]
|
||||||
else:
|
quote do:
|
||||||
T.none
|
block:
|
||||||
safeGet(`expression`, `index`)
|
safeGet(`expression`, `index`)
|
||||||
|
|
||||||
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]
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user