diff options
author | Andreas Rumpf <rumpf_a@web.de> | 2022-10-07 22:26:53 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-10-07 22:26:53 +0200 |
commit | e83f27e6a0f52f167e8eb91cd8f60be62d6725c6 (patch) | |
tree | 5e23e0c54abe38038405992deea1c234030d49b1 /lib/std/outparams.nim | |
parent | a132f5502acbd53781802579d89d6ca5168e74cd (diff) | |
download | Nim-e83f27e6a0f52f167e8eb91cd8f60be62d6725c6.tar.gz |
out parameters: enforce that 'out' is only used as a parameter (#20510)
* out parameters: enforce that 'out' is only used as a parameter * make tables.nim use 'out' parameters * better backwards compat
Diffstat (limited to 'lib/std/outparams.nim')
-rw-r--r-- | lib/std/outparams.nim | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/lib/std/outparams.nim b/lib/std/outparams.nim new file mode 100644 index 000000000..8a0e5ae67 --- /dev/null +++ b/lib/std/outparams.nim @@ -0,0 +1,38 @@ +# +# +# Nim's Runtime Library +# (c) Copyright 2022 Nim contributors +# +# See the file "copying.txt", included in this +# distribution, for details about the copyright. +# + +## `outParamsAt` macro for easy writing code that works with both 2.0 and 1.x. + +import macros + +macro outParamsAt*(positions: static openArray[int]; n: untyped): untyped = + ## Use this macro to annotate `out` parameters in a portable way. + runnableExamples: + proc p(x: var int) {.outParamsAt: [1].} = + discard "x is really an 'out int' if the Nim compiler supports 'out' parameters" + + result = n + when defined(nimHasOutParams): + var p = n.params + for po in positions: + p[po][^2].expectKind nnkVarTy + p[po][^2] = newTree(nnkOutTy, p[po][^2][0]) + +when isMainModule: + {.experimental: "strictDefs".} + + proc main(x: var int) {.outParamsAt: [1].} = + x = 3 + + proc us = + var x: int + main x + echo x + + us() |