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

Concept; cannot access fields in iterator #18780

Open
enthus1ast opened this issue Sep 2, 2021 · 1 comment
Open

Concept; cannot access fields in iterator #18780

enthus1ast opened this issue Sep 2, 2021 · 1 comment
Labels

Comments

@enthus1ast
Copy link
Contributor

Example

type
  Loopable*[T] = concept x
    x.len() is int
    x[int] is T
    x.items is T

iterator loop*[T](a: Loopable[T]): T = # TODO cannot access fields in iterator
  for each in a:
    yield each

type
  Elem* = object
    baa*: string

proc foo(elems: seq[Elem]): string =
  for elem in elems.loop():
    result &= "\n  "
    result &= $(elem.baa) # TODO Error: undeclared field: 'baa'
    result &= "\n"

let elems = @[Elem(baa: "one"), Elem(baa: "two")]
echo foo(elems)

Current Output

Error: undeclared field: 'baa'

Expected Output

one

two

Additional Information

nim --version
Nim Compiler Version 1.4.8 [Windows: amd64]
Compiled at 2021-05-25
Copyright (c) 2006-2021 by Andreas Rumpf

nim --version
Nim Compiler Version 1.5.1 [Windows: amd64]
Compiled at 2021-09-01
Copyright (c) 2006-2021 by Andreas Rumpf

@enthus1ast
Copy link
Contributor Author

workaround:

proc foo(elems: seq[Elem]): string =
  for elem in elems.loop():
    result &= "\n  "
    result &= $(elem.Elem.baa) # <----------------
    result &= "\n"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants