There is already an extension function to iterate over the elements of an iterator using forEach
.
Assuming you want to use objects that do not implement Iterator
but do have the methods of Iterator
, as an iterator, this has been discussed before. The Kotlin team decided not to implement it: "Extension types" for Kotlin. Also see Implicit interfaces as in Go and Implicit interfaces.