https://kotlinlang.org logo
Title
l

locke

12/10/2018, 5:19 PM
Is it possible to create a static extension function to a Java class? It doesn't have a Companion to attach it to, but the non-static version is genericized?
s

Shawn

12/10/2018, 5:20 PM
Extension functions are all static, and yes, you can write extension functions for Java classes
l

locke

12/10/2018, 5:22 PM
Then what would the signature be?
fun <Class>.<function>(<arguments>)
yields a request for a type argument from the compiler and,
<Class>.Companion.<function>(<arguments>)
yields an unresolved reference.
s

Shawn

12/10/2018, 5:23 PM
I think I might’ve misunderstood what you meant, but let’s tackle the syntax first
fun JavaClass.function(args) { }
should work just fine - where do the type arguments come into play?
Also, technically you can define a generic extension function like this
fun <T> T.function(args) { }
l

locke

12/10/2018, 5:25 PM
The fact that
JavaClass
is genericized, and so the compiler requests a type argument, i.e. it's scoping that function to only
JavaClass<T>
. I am trying to get it to work with just
JavaClass.function...
without needing to give it a type argument.
s

Shawn

12/10/2018, 5:25 PM
Ah, I see what you mean now
Should just be able to use star projection here if the type isn’t relevant
fun JavaClass<*>.function()
l

locke

12/10/2018, 5:28 PM
I am currently trying -- using that is losing access to static operators. For context, I'm trying to add a composite function to
Observable
in RxJava. It's using both
fromIterable
and
interval
that are static on
Observable
. I guess since they're static I can just use
Observable.fromIterable
instead of
this.fromIterable
but that certainly feels like a code smell
s

Shawn

12/10/2018, 5:29 PM
why is that a code smell? Calling static methods by way of an instance is the code smell
l

locke

12/10/2018, 5:30 PM
In my brain
this
should refer to the
Companion
in this context and thus those methods should be available in scope without needing to define it. I.e. just calling
fromIterable
which is implicitly
this.fromIterable
But I guess that's not how it works
Though interestingly this method doesn't actually work, it seems?
Observable.function
doesn't expose this extension.
s

Shawn

12/10/2018, 5:32 PM
expose in what sense?
l

locke

12/10/2018, 5:34 PM
You can't call it in client code.
Observable.function
doesn't actually reference the
Observable<*>.function
declaration. I'm sure if I had an instance of
Observable
I would have access, but that's not the goal.
s

Shawn

12/10/2018, 5:35 PM
Are you trying to call extension methods from Java?
k

karelpeeters

12/10/2018, 5:36 PM
@Shawn I may be misunderstanding but I think you missed that he wants static extension functions.
s

Shawn

12/10/2018, 5:36 PM
Ahh, as though they were defined as a static method within the class body
l

locke

12/10/2018, 5:36 PM
@Shawn, no I'm calling them from Kotlin. The goal is to be able to write an extension function such that I can in Kotlin declare:
Observable.myFunction(<arguments>)
Yes, exactly
s

Shawn

12/10/2018, 5:37 PM
that was my bad
l

locke

12/10/2018, 5:37 PM
All good, technical problems are hard to communicate 🙂
👍 1
Is this even possible? I am not sure it is
s

Shawn

12/10/2018, 5:38 PM
Unfortunately I don’t think it is 😕
k

karelpeeters

12/10/2018, 5:38 PM
No, same for Kotlin classes that don't define a companion themselves.
l

locke

12/10/2018, 5:39 PM
That's unfortunate. Oh well, thanks guys!
g

gildor

12/11/2018, 12:38 AM
Please vote for this feature and share your use cases https://youtrack.jetbrains.com/issue/KT-11968
👏 1