Nim/lib/std/pragmas.nim
Timothee Cour 913bc95964
new syntax for lvalue references: var b {.byaddr.} = expr (#13508)
* new syntax for lvalue references: `var b {.byaddr.} = expr`
* on type mismatch, `???(0, 0)` not shown anymore
* * compiler now lowers `var a: {.foo.}: MyType = expr` to foo(a, MyType, expr)
* new pragmas.byaddr defined in pure library code exploiting this lowering
* skip `template foo() {.pragma.}`
2020-03-23 11:15:45 +01:00

19 lines
535 B
Nim

# see `semLowerLetVarCustomPragma` for compiler support that enables these
# lowerings
template byaddr*(lhs, typ, expr) =
## Allows a syntax for lvalue reference, exact analog to
## `auto& a = expr;` in C++
runnableExamples:
var s = @[10,11,12]
var a {.byaddr.} = s[0]
a+=100
doAssert s == @[110,11,12]
doAssert a is int
var b {.byaddr.}: int = s[0]
doAssert a.addr == b.addr
when typ is type(nil):
let tmp = addr(expr)
else:
let tmp: ptr typ = addr(expr)
template lhs: untyped = tmp[]