Created
November 30, 2018 20:08
-
-
Save nepeckman/7bbbb1b2b1adbf584b6e48a90ac6b286 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import options, typetraits, sugar, typeinfo | |
template `?`(T: typedesc): typedesc = | |
Option[T] | |
type | |
Bar = ref object | |
baz: int | |
Foo = ref object | |
bar: ?Bar | |
mapper[T, R] = proc (val: T): R {.noSideEffect, gcSafe, locks: 0.} | |
func bar(foo: Foo): ?Bar = foo.bar | |
func baz(bar: Bar): int = bar.baz | |
template `?.?`[T](a: Option[T], b: untyped): untyped = | |
a.flatMap(proc (val: T): untyped = val.b) | |
template `?.`[T](a: Option[T], b: untyped): untyped = | |
a.map(proc (val: T): untyped = val.b) | |
let foo1: ?Foo = some(Foo(bar: some(Bar(baz: 10)))) | |
let foo2: ?Foo = none[Foo]() | |
let foo3: Foo = Foo(bar: none[Bar]()) | |
discard `?.?`(foo1, bar) | |
echo foo2?.?bar?.baz | |
echo foo3.bar?.baz |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment