Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix docs + incorrect return types for fieldPairs, fields #13189

Merged
merged 1 commit into from
Jan 18, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 40 additions & 26 deletions lib/system/iterators.nim
Original file line number Diff line number Diff line change
Expand Up @@ -217,58 +217,72 @@ iterator mitems*(a: var string): var char {.inline.} =


iterator fields*[T: tuple|object](x: T): RootObj {.
magic: "Fields", noSideEffect.}
magic: "Fields", noSideEffect.} =
## Iterates over every field of `x`.
##
## **Warning**: This really transforms the 'for' and unrolls the loop.
## The current implementation also has a bug
## that affects symbol binding in the loop body.
iterator fields*[S:tuple|object, T:tuple|object](x: S, y: T): tuple[a, b: RootObj] {.
magic: "Fields", noSideEffect.}
runnableExamples:
var t = (1, "foo")
for v in fields(t): v = default(type(v))
doAssert t == (0, "")

iterator fields*[S:tuple|object, T:tuple|object](x: S, y: T): tuple[key: string, val: RootObj] {.
magic: "Fields", noSideEffect.} =
## Iterates over every field of `x` and `y`.
##
## **Warning**: This really transforms the 'for' and unrolls the loop.
## The current implementation also has a bug that affects symbol binding
## in the loop body.
iterator fieldPairs*[T: tuple|object](x: T): RootObj {.
magic: "FieldPairs", noSideEffect.}
runnableExamples:
var t1 = (1, "foo")
var t2 = default(type(t1))
for v1, v2 in fields(t1, t2): v2 = v1
doAssert t1 == t2

iterator fieldPairs*[T: tuple|object](x: T): tuple[key: string, val: RootObj] {.
magic: "FieldPairs", noSideEffect.} =
## Iterates over every field of `x` returning their name and value.
##
## When you iterate over objects with different field types you have to use
## the compile time ``when`` instead of a runtime ``if`` to select the code
## you want to run for each type. To perform the comparison use the `is
## operator <manual.html#generics-is-operator>`_. Example:
##
## .. code-block:: Nim
## type
## Custom = object
## foo: string
## bar: bool
##
## proc `$`(x: Custom): string =
## result = "Custom:"
## for name, value in x.fieldPairs:
## when value is bool:
## result.add("\n\t" & name & " is " & $value)
## else:
## if value.isNil:
## result.add("\n\t" & name & " (nil)")
## else:
## result.add("\n\t" & name & " '" & value & "'")
##
## operator <manual.html#generics-is-operator>`_.
## Another way to do the same without ``when`` is to leave the task of
## picking the appropriate code to a secondary proc which you overload for
## each field type and pass the `value` to.
##
## **Warning**: This really transforms the 'for' and unrolls the loop. The
## current implementation also has a bug that affects symbol binding in the
## loop body.
runnableExamples:
type
Custom = object
foo: string
bar: bool
proc `$`(x: Custom): string =
result = "Custom:"
for name, value in x.fieldPairs:
when value is bool:
result.add("\n\t" & name & " is " & $value)
else:
result.add("\n\t" & name & " '" & value & "'")

iterator fieldPairs*[S: tuple|object, T: tuple|object](x: S, y: T): tuple[
a, b: RootObj] {.
magic: "FieldPairs", noSideEffect.}
key: string, a, b: RootObj] {.
magic: "FieldPairs", noSideEffect.} =
## Iterates over every field of `x` and `y`.
##
## **Warning**: This really transforms the 'for' and unrolls the loop.
## The current implementation also has a bug that affects symbol binding
## in the loop body.
runnableExamples:
type Foo = object
x1: int
x2: string
var a1 = Foo(x1: 12, x2: "abc")
var a2: Foo
for name, v1, v2 in fieldPairs(a1, a2):
when name == "x2": v2 = v1
doAssert a2 == Foo(x1: 0, x2: "abc")