summary refs log tree commit diff stats
path: root/lib/std
diff options
context:
space:
mode:
Diffstat (limited to 'lib/std')
-rw-r--r--lib/std/private/underscored_calls.nim25
-rw-r--r--lib/std/with.nim7
2 files changed, 19 insertions, 13 deletions
diff --git a/lib/std/private/underscored_calls.nim b/lib/std/private/underscored_calls.nim
index c7aeb6ae2..f0bcbcc74 100644
--- a/lib/std/private/underscored_calls.nim
+++ b/lib/std/private/underscored_calls.nim
@@ -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
-  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:
+  expectKind calls, {nnkArgList, nnkStmtList, nnkStmtListExpr}
+
+  for call in calls:
+    if call.kind in {nnkStmtList, nnkStmtListExpr}:
+      underscoredCalls(result, call, arg0)
+    else:
       result.add underscoredCall(call, arg0)
diff --git a/lib/std/with.nim b/lib/std/with.nim
index c1ac96fcb..ea26065a1 100644
--- a/lib/std/with.nim
+++ b/lib/std/with.nim
@@ -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