-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
Showing
5 changed files
with
47 additions
and
3 deletions.
There are no files selected for viewing
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() |