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:
parent
957bf15a08
commit
ba042af9cc
4 changed files with 59 additions and 12 deletions
|
|
@ -337,6 +337,32 @@ macro collect*(init, body: untyped): untyped {.since: (1, 1).} =
|
|||
|
||||
when isMainModule:
|
||||
since (1, 1):
|
||||
block dup_with_field:
|
||||
type
|
||||
Foo = object
|
||||
col, pos: int
|
||||
name: string
|
||||
|
||||
proc inc_col(foo: var Foo) = inc(foo.col)
|
||||
proc inc_pos(foo: var Foo) = inc(foo.pos)
|
||||
proc name_append(foo: var Foo, s: string) = foo.name &= s
|
||||
|
||||
let a = Foo(col: 1, pos: 2, name: "foo")
|
||||
block:
|
||||
let b = a.dup(inc_col, inc_pos):
|
||||
_.pos = 3
|
||||
name_append("bar")
|
||||
inc_pos
|
||||
|
||||
doAssert(b == Foo(col: 2, pos: 4, name: "foobar"))
|
||||
|
||||
block:
|
||||
let b = a.dup(inc_col, pos = 3, name = "bar"):
|
||||
name_append("bar")
|
||||
inc_pos
|
||||
|
||||
doAssert(b == Foo(col: 2, pos: 4, name: "barbar"))
|
||||
|
||||
import algorithm
|
||||
|
||||
var a = @[1, 2, 3, 4, 5, 6, 7, 8, 9]
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue