https://kotlinlang.org logo
#announcements
Title
# announcements
s

supaham

07/23/2017, 7:57 PM
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

diesieben07

07/23/2017, 7:59 PM
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

supaham

07/23/2017, 8:01 PM
Are there solutions (other than #1) that do with the method signature?
d

diesieben07

07/23/2017, 8:03 PM
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

supaham

07/23/2017, 8:03 PM
But isnt an array an iterable?
d

diesieben07

07/23/2017, 8:03 PM
No, it is not.
Complain to Java 😛
s

supaham

07/23/2017, 8:03 PM
I know it isnt in Kotlin, but java it is right? isnt that how old school
for
is possible during compile-time
d

diesieben07

07/23/2017, 8:04 PM
No. The foreach-loop on arrays is special cased, it does not compile to an
iterator
method call.
s

supaham

07/23/2017, 8:04 PM
Ah
Well #3 is my solution 🙂 Thanks
3 Views