Why doesn't CharSequence implement Iterable?


It can be very handy to iterate over a String as with a Collection. However Kotlin’s CharSequence doesn’t implement Iterable and it seems there is no good reason (but maybe I’ve missed something). So, why doesn’t CharSequence implement Iterable?


Because java.lang.CharSequence doesn’t implement java.lang.Iterable, and we can’t retrofit a super-interface to a Java interface that doesn’t implement it.


This decision could be reconsidered for other target platforms, where there are no such interop restrictions.


@medium, could you provide an example, what would become more convenient than it is now in Kotlin, if CharSequence implemented an Iterable?


But it is core api. This may break multitarget code. Like Math did.


@ilya.gorbunov I’m writing some (more or less common) extension functions where the lowest common denominator would be Iterable. For example my logic is based on the return value of contains and is the same no matter if it is a String or a List.

Maybe I’ll just use the extension function asIterable and add one more extension function which converts the CharSequence first and the calls the real method then. However such helpers add up over time.