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

@ -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]