Any easy way to make a class implementing `operato...
# announcements
d
Any easy way to make a class implementing
operator iterator()
that returns a custom iterator that implements
operator hasNext()
and
operator next()
, to return a List? It seems that currently the only way is to make a mutable list and then
for (e in iterator) list.add(e)
...?
f
I think you want to use
List
constructor
val list = List(count, { index -> valueFor(index) })
or maybe there's a way to instantiate the list via the iterator
d
I'm asking because of the ReceiveChannel in coroutines, but it doesn't implement Iterable, it just defines operators for
in
to work...
@fourlastor The List constructor won't help, since I don't have indexed retrieval only hasNext and next operators...
f
you can create an iterable with
Iterable
, let me look up the function
and then use
toList()
d
The definition of the function is:
Copy code
public inline fun <T> Iterable(crossinline iterator: () -> Iterator<T>): Iterable<T> = object : Iterable<T> {
    override fun iterator(): Iterator<T> = iterator()
}
Doesn't help in this case... since my iterator() function doesn't return a real Iterable, oh well I guess back to the harder way... 😢