mirror of
https://github.com/logos-storage/questionable.git
synced 2026-01-03 06:13:09 +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/options
|
||||
|
||||
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.low and `index` <= evaluated.high:
|
||||
evaluated[`index`].some
|
||||
else:
|
||||
T.none
|
||||
proc safeGet[T](expression: seq[T] | openArray[T], index: int): Option[T] =
|
||||
if index >= expression.low and index <= expression.high:
|
||||
expression[index].some
|
||||
else:
|
||||
T.none
|
||||
|
||||
macro `.?`*(expression: openArray, brackets: untyped{nkBracket}): untyped =
|
||||
# chain is of shape: openArray.?[index]
|
||||
let index = brackets[0]
|
||||
quote do:
|
||||
block:
|
||||
type T = typeof(`expression`[`index`])
|
||||
proc safeGet(arr: openArray[T], i: int): ?T {.gensym.} =
|
||||
if i >= arr.low and i <= arr.high:
|
||||
arr[i].some
|
||||
else:
|
||||
T.none
|
||||
safeGet(`expression`, `index`)
|
||||
proc safeGet(expression: string, index: int): Option[char] =
|
||||
if index >= expression.low and index <= expression.high:
|
||||
expression[index].some
|
||||
else:
|
||||
char.none
|
||||
|
||||
macro `.?`*(expression: seq | string | openArray, brackets: untyped{nkBracket}): untyped =
|
||||
# chain is of shape: (seq or string or openArray).?[index]
|
||||
let index = brackets[0]
|
||||
quote do:
|
||||
block:
|
||||
safeGet(`expression`, `index`)
|
||||
|
||||
macro `.?`*(expression: typed, brackets: untyped{nkBracket}): untyped =
|
||||
# chain is of shape: expression.?[index]
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user