https://kotlinlang.org logo
Title
a

aerb

12/29/2017, 10:41 PM
Hey, is there a way to call an extension method with appropriate namespace, as you would with a static method? Basically to deal with name conflicts?
d

damian

12/29/2017, 10:42 PM
how do you imagine it to be?
extension functions should already be in their respective namespace (extending the class)
a

aerb

12/29/2017, 10:45 PM
yeah, it's kind of a weird question. This would be to resolve an extension function conflict due to subclassing.
It's a weird scenario though that maybe indicates something is wrong.
d

damian

12/29/2017, 10:46 PM
can you post an example?
a

aerb

12/29/2017, 10:46 PM
I wanted to write and extension to a Json base base class which implicitely casts to a JsonArray, and then maps the values.
inline fun <T> JsonElement?.map(transform: (JsonElement) -> T): List<T> =
    (this as JsonArray).map(transform)
JsonArray implements list, which is what I want to use, but it just ends up being recursive.
inline fun <T> JsonElement?.map(transform: (JsonElement) -> T): List<T> =
    (this as JsonArray as Iterable<JsonElement>).map(transform)
That works, but seems a bit weird.
a

araqnid

12/29/2017, 10:49 PM
you might as well take out the
as JsonArray
in that one?
a

aerb

12/29/2017, 10:50 PM
yeah, I can I can do that. It gives a casting warning, which is the only reason I was doing the two casts, but I guess that doesn't really matter.
I guess I was hoping for a
.map@kotlin.collections(transform)
, or something like that.
a

araqnid

12/29/2017, 10:51 PM
tbh I’d be more inclined to have a separate method to cast it to an array, as that can fail, e.g.
JsonElement?.asArray() = this as JsonArray
I understand what you mean, but I haven’t seen any way of doing that
a

aerb

12/29/2017, 10:52 PM
yeah, not a big deal, but figured i'd check. Thanks.