std/with support field assign (#14484)

* std/with support filed assign

* add changelog

* add support x.dup.with

* add example

* revert support x.dup.with; add example

* update changelog; fix assignment in parameter

* Update changelog.md

* add example for assignment in parameter

* Remove colon style assign

Co-authored-by: Clyybber <darkmine956@gmail.com>
This commit is contained in:
slangmgh 2020-08-15 07:33:21 +08:00 • committed by GitHub
commit ba042af9cc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 59 additions and 12 deletions

View file

@ -26,6 +26,12 @@ proc underscoredCall(n, arg0: NimNode): NimNode =
for i in 1..u-1: result.add n[i]
result.add arg0
for i in u+1..n.len-1: result.add n[i]
elif n.kind in {nnkAsgn, nnkExprEqExpr}:
var field = n[0]
if n[0].kind == nnkDotExpr and n[0][0].eqIdent("_"):
# handle _.field = ...
field = n[0][1]
result = newDotExpr(arg0, field).newAssignment n[1]
else:
# handle e.g. 'x.dup(sort)'
result = newNimNode(nnkCall, n)
@ -33,17 +39,10 @@ proc underscoredCall(n, arg0: NimNode): NimNode =
result.add arg0
proc underscoredCalls*(result, calls, arg0: NimNode) =
proc handleStmtList(result, n, arg0: NimNode) =
for a in n:
if a.kind in {nnkStmtList, nnkStmtListExpr}:
handleStmtList(result, a, arg0)
else:
result.add underscoredCall(a, arg0)
expectKind calls, {nnkArgList, nnkStmtList, nnkStmtListExpr}
expectKind calls, nnkArgList
if calls.len == 1 and calls[0].kind in {nnkStmtList, nnkStmtListExpr}:
# the 'macro: body' syntax is used:
handleStmtList(result, calls[0], arg0)
else:
for call in calls:
for call in calls:
if call.kind in {nnkStmtList, nnkStmtListExpr}:
underscoredCalls(result, call, arg0)
else:
result.add underscoredCall(call, arg0)

View file

@ -41,6 +41,7 @@ when isMainModule:
type
Foo = object
col, pos: string
name: string
proc setColor(f: var Foo; r, g, b: int) = f.col = $(r, g, b)
proc setPosition(f: var Foo; x, y: float) = f.pos = $(x, y)
@ -49,3 +50,9 @@ when isMainModule:
with(f, setColor(2, 3, 4), setPosition(0.0, 1.0))
echo f
f = Foo()
with f:
col = $(2, 3, 4)
pos = $(0.0, 1.0)
_.name = "bar"
echo f