diff options
author | slangmgh <37659406+slangmgh@users.noreply.github.com> | 2020-08-15 07:33:21 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-08-15 01:33:21 +0200 |
commit | ba042af9cc6194cd3c3e67e5daf0165b4b3630c0 (patch) | |
tree | 960fe34914af04ed3cacb294cd70b5858a49b3f8 /lib/pure | |
parent | 957bf15a08d5443a50452909743747d19b5f29f8 (diff) | |
download | Nim-ba042af9cc6194cd3c3e67e5daf0165b4b3630c0.tar.gz |
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>
Diffstat (limited to 'lib/pure')
-rw-r--r-- | lib/pure/sugar.nim | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/lib/pure/sugar.nim b/lib/pure/sugar.nim index 00f4de5d8..0edcc5697 100644 --- a/lib/pure/sugar.nim +++ b/lib/pure/sugar.nim @@ -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] |