if I have a `fun args(args: Iterable<String>...
# announcements
s
if I have a
fun args(args: Iterable<String>)
and i pass it an Array<String> it says array is not a subtype of iterable. What's the solution to this problem?
d
Several possibilities. 1 - Make
args
of type
Array<String>
. 2 - Pass something that is actually an
Iterable<String>
, e.g.
List<String>
. 3 - Use
Array::asIterable
.
👍 1
s
Are there solutions (other than #1) that do with the method signature?
d
No, if the method signature declares an
Iterable
you cannot pass something that's not an
Iterable
. That's like passing a
String
where a
List
is expected. The types do not match up.
s
But isnt an array an iterable?
d
No, it is not.
Complain to Java 😛
s
I know it isnt in Kotlin, but java it is right? isnt that how old school
for
is possible during compile-time
d
No. The foreach-loop on arrays is special cased, it does not compile to an
iterator
method call.
s
Ah
Well #3 is my solution 🙂 Thanks