Stream: beginners

Topic: Iterator type with the new compiler


view this post on Zulip Matthieu Pizenberg (Dec 11 2025 at 23:18):

I’ve read that iterators are not yet part of the new compiler but I’m curious, if you had to define an Iter type with what exists today in the new compiler, how would you define it?

For example, in elm, I’d probably define the type like this:

type Iter a s
    = Iter
        { state : s
        , next_ : Next a s
        }

type alias Next a s =
    s -> Maybe ( a, s )

next : Iter a s -> Maybe a
next (Iter { state, next_ }) =
    Maybe.map Tuple.first (next_ state)

step : Iter a s -> ( Maybe a, Iter a s )
step ((Iter { state, next_ }) as iter) =
    case next_ state of
        Just ( a, newState ) ->
            ( Just a, Iter { state = newState, next_ = next_ } )

        Nothing ->
            ( Nothing, iter )

view this post on Zulip Matthieu Pizenberg (Dec 11 2025 at 23:19):

I’m asking because I wanted to experiment a bit with it, but struggling a bit with the type definitions ^^.

view this post on Zulip Luke Boswell (Dec 11 2025 at 23:28):

You may find this older thread interesting #ideas > Iterators and fusion proposal @ 💬

view this post on Zulip Matthieu Pizenberg (Dec 11 2025 at 23:33):

interesting, though it doesn’t seem to be up-to-date with the current syntax. Do you have example codebases with lots of different type definitions? I don’t think there are any records or type aliases in Builtin.roc for example.

view this post on Zulip Luke Boswell (Dec 11 2025 at 23:37):

There are many examples in the test/snapshots directory which are using the current syntax

view this post on Zulip Luke Boswell (Dec 11 2025 at 23:38):

Nothing on Iter specifically -- I'm not sure if static dispatch would change the semantics significantly, or if it's just updating the syntax to use parens and commas etc

view this post on Zulip Matthieu Pizenberg (Dec 12 2025 at 00:04):

For example, when I try this:

Iter(item, s) := [MyIter({ state: s, next: s -> Try((item, s), [Empty]) })].{
    next = |MyIter(myiter)| {
        myiter.next(myiter.state)
    }
}

I get this error

-- MISSING METHOD --------------------------------

This next method is being called on a value whose type doesn't have that method:
    ┌─ day03.roc:111:16
    │
111 │         myiter.next(myiter.state)
    │                ^^^^

The value's type, which does not have a method named next, is:

    { state: _field }

I haven’t found much examples in the snapshots. If you have specific files in mind, let me know.

view this post on Zulip Matthieu Pizenberg (Dec 12 2025 at 00:08):

Ah I got this one!

Iter(item, s) := [MyIter({ state: s, next: s -> Try((item, s), [Empty]) })].{
    next : Iter(item, s) -> Try((item, s), [Empty])
    next = |MyIter(myiter)| {
        (myiter.next)(myiter.state)
    }
}

view this post on Zulip Matthieu Pizenberg (Dec 12 2025 at 00:10):

Is there a way to make a type opaque?

view this post on Zulip Richard Feldman (Dec 12 2025 at 00:14):

:: instead of :=


Last updated: Dec 21 2025 at 12:15 UTC