https://kotlinlang.org logo
#android
Title
# android
a

arekolek

08/18/2017, 3:45 PM
Wouldn’t this work?
Copy code
extras
    .getParcelableArray(LocationPicker.INPUT_POLYGON)
    .filterIsInstance<LatLng>()
    .forEach(inputPolygon1::add)
n

nickk

08/18/2017, 3:55 PM
This would work (I guess), but still not much better.
a

arekolek

08/18/2017, 3:57 PM
what do you hate about it?
You could define an extension function like
inline fun <reified T> Bundle.getTypedArray(key: String) = getParcelableArray(key).filterIsInstance<T>()
If there’s
addAll
in your polygon, you could then do:
inputPolygon1.addAll(extras.getTypedArray<LatLng>(LocationPicker.INPUT_POLYGON))
Define one, if there’s not.
Not sure if it can be less horrible than that, but I’m curious
n

nickk

08/18/2017, 4:04 PM
Of course your code is a lot better. But doesn’t it still feel like a workaround?
a

arekolek

08/18/2017, 4:12 PM
I guess that depends on your intention. If you are sure those are all LatLng (you control the code that creates the parcelable array) you’d be better off with suppressing the unchecked warning (like here https://kotlinlang.slack.com/archives/C0B8M7BUY/p1503070876000405 ). If you want to make sure all items are LatLngs, it might be better to do something like
Copy code
inline fun <reified R> Iterable<*>.mapAsInstance() = map { it.apply { check(this is R) } as R }
instead of dropping those items silently. (see also https://stackoverflow.com/a/36570969 )
13 Views