Skip to content

[BUG] Not sure how to do iterators #298

@victor-Lopez25

Description

@victor-Lopez25

How should I make iterators? I tried doing something similar to std/string.zc but I couldn't get it to work:

struct StringViewLineIter {
    sv: StringView;
}

impl StringViewLineIter {
    fn next(self) -> Option<StringView> {
        if(self.sv.len == 0) return Option<StringView>::None();

        let line = self.sv.chop_by_line();
        return Option<StringView>::Some(line);
    }
}

usage is:

    let sv4 = StringView::from("""
struct StringViewLineIter {{
    sv: StringView;
}}

impl StringViewLineIter {{
    fn next(self) -> Option<StringView> {{
        if(self.sv.len == 0) return Option<StringView>::None();

        let line = self.sv.chop_by_line();
        return Option<StringView>::Some(line);
    }}
}}
""");

    for l in sv4.get_line_iterator() {
        printf("%.*s\n", l.len, l.data);
    }

getting this error:

main.c: In function 'test_string_view':
main.c:3821:20: error: implicit declaration of function 'StringViewLineIter__iterator'; did you mean 'StringViewLineIter__next'? [-Wimplicit-function-declaration]
 3821 |     ZC_AUTO __it = StringViewLineIter__iterator((StringViewLineIter[]){StringView__get_line_iterator((&sv4))});
      |                    ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
      |                    StringViewLineIter__next
main.c:3823:25: error: request for member 'next' in something not a structure or union
 3823 |     ZC_AUTO __opt = __it.next();
      |                         ^
main.c:3824:5: error: '__opt' undeclared (first use in this function)
 3824 | if (__opt.is_none()) break;
      |     ^~~~~
main.c:3824:5: note: each undeclared identifier is reported only once for each function it appears in
main.c:3827:1: error: 'l' undeclared (first use in this function)
 3827 | l.println();
      | ^
error: C compilation failed

Environment (please complete the following information):

  • OS: Windows
  • Zen C Version: zc v0.4.2-108-g4ffbcb1-dirty
  • Compiler: MingwGCC

Additional context
here's the full code for reference: https://github.com/victor-Lopez25/zc-serialize/blob/master/string_view.zc

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    Status

    Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions