diff options
author | metagn <metagngn@gmail.com> | 2022-08-23 20:44:37 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-08-23 19:44:37 +0200 |
commit | f6eb1d4d7d09eee1c366eff44437034e12bbb099 (patch) | |
tree | 25ccd3d20b839ee46fd83cd456c75aaddc51434e /tests/misc | |
parent | 3dbf2ac9469bdaaf876f902242a883fe1847adf0 (diff) | |
download | Nim-f6eb1d4d7d09eee1c366eff44437034e12bbb099.tar.gz |
remove {.this.} pragma, deprecated since 0.19 (#20201)
Co-authored-by: Andreas Rumpf <rumpf_a@web.de>
Diffstat (limited to 'tests/misc')
-rw-r--r-- | tests/misc/tcsharpusingstatement.nim | 88 | ||||
-rw-r--r-- | tests/misc/tupcomingfeatures.nim | 35 |
2 files changed, 88 insertions, 35 deletions
diff --git a/tests/misc/tcsharpusingstatement.nim b/tests/misc/tcsharpusingstatement.nim new file mode 100644 index 000000000..dd4cf589d --- /dev/null +++ b/tests/misc/tcsharpusingstatement.nim @@ -0,0 +1,88 @@ +discard """ + output: "Using test.Closing test." +""" + +import + macros + +# This macro mimics the using statement from C# +# +# It's kept only as a test for the macro system +# Nim's destructors offer a mechanism for automatic +# disposal of resources. +# +macro autoClose(args: varargs[untyped]): untyped = + let e = callsite() + if e.len != 3: + error "Using statement: unexpected number of arguments. Got " & + $e.len & ", expected: 1 or more variable assignments and a block" + + var args = e + var body = e[2] + + var + variables : seq[NimNode] + closingCalls : seq[NimNode] + + newSeq(variables, 0) + newSeq(closingCalls, 0) + + for i in countup(1, args.len-2): + if args[i].kind == nnkExprEqExpr: + var varName = args[i][0] + var varValue = args[i][1] + + var varAssignment = newNimNode(nnkIdentDefs) + varAssignment.add(varName) + varAssignment.add(newNimNode(nnkEmpty)) # empty means no type + varAssignment.add(varValue) + variables.add(varAssignment) + + closingCalls.add(newCall(newIdentNode("close"), varName)) + else: + error "Using statement: Unexpected expression. Got " & + $args[i].kind & " instead of assignment." + + var varSection = newNimNode(nnkVarSection) + varSection.add(variables) + + var finallyBlock = newNimNode(nnkStmtList) + finallyBlock.add(closingCalls) + + # XXX: Use a template here once getAst is working properly + var targetAst = parseStmt"""block: + var + x = foo() + y = bar() + + try: + body() + + finally: + close x + close y + """ + + targetAst[0][1][0] = varSection + targetAst[0][1][1][0] = body + targetAst[0][1][1][1][0] = finallyBlock + + result = targetAst + +type + TResource* = object + field*: string + +proc openResource(param: string): TResource = + result.field = param + +proc close(r: var TResource) = + write(stdout, "Closing " & r.field & ".") + +proc use(r: var TResource) = + write(stdout, "Using " & r.field & ".") + +autoClose(r = openResource("test")): + use r + +write stdout, "\n" diff --git a/tests/misc/tupcomingfeatures.nim b/tests/misc/tupcomingfeatures.nim deleted file mode 100644 index d37ce85cf..000000000 --- a/tests/misc/tupcomingfeatures.nim +++ /dev/null @@ -1,35 +0,0 @@ -discard """ - output: '''0 -2 0 -0 -2''' -""" - -{.this: self.} - -type - Foo = object - a, b, x: int - -proc yay(self: Foo) = - echo a, " ", b, " ", x - -proc footest[T](self: var Foo, a: T) = - b = 1+a - yay() - -proc nongeneric(self: Foo) = - echo a, " ", b - -var ff: Foo -footest(ff, -3) -ff.nongeneric - -{.experimental.} -using - c: Foo - x, y: int - -proc usesSig(c) = - echo "yummy" - -proc foobar(c, y) = - echo "yay" |