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 )
I’m asking because I wanted to experiment a bit with it, but struggling a bit with the type definitions ^^.
You may find this older thread interesting
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.
There are many examples in the test/snapshots directory which are using the current syntax
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
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.
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)
}
}
Is there a way to make a type opaque?
:: instead of :=
Last updated: Dec 21 2025 at 12:15 UTC