Skip to content

Concept; cannot access fields in iterator #18780

@enthus1ast

Description

@enthus1ast

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions