summary refs log tree commit diff stats
path: root/lib/std
diff options
context:
space:
mode:
authorTimothee Cour <timothee.cour2@gmail.com>2020-03-23 03:15:45 -0700
committerGitHub <noreply@github.com>2020-03-23 11:15:45 +0100
commit913bc95964458d3eb2fc4d8a108031e6b9398da5 (patch)
treef66dc374d3ff53ff444e4975cfd40ec14916291c /lib/std
parentfa06203e90d9bb9211d0b6b9726fc9f2c5dc80ad (diff)
downloadNim-913bc95964458d3eb2fc4d8a108031e6b9398da5.tar.gz
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.}`
Diffstat (limited to 'lib/std')
-rw-r--r--lib/std/pragmas.nim19
1 files changed, 19 insertions, 0 deletions
diff --git a/lib/std/pragmas.nim b/lib/std/pragmas.nim
new file mode 100644
index 000000000..5dce04c11
--- /dev/null
+++ b/lib/std/pragmas.nim
@@ -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[]