mirror of
https://github.com/nim-lang/Nim.git
synced 2026-04-19 05:50:30 +00:00
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.}`
This commit is contained in:
19
lib/std/pragmas.nim
Normal file
19
lib/std/pragmas.nim
Normal file
@@ -0,0 +1,19 @@
|
||||
# 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[]
|
||||
Reference in New Issue
Block a user