Is it possible to create a static extension functi...
# announcements
l
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
Extension functions are all static, and yes, you can write extension functions for Java classes
l
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
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
Copy code
fun <T> T.function(args) { }
l
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
Ah, I see what you mean now
Should just be able to use star projection here if the type isn’t relevant
Copy code
fun JavaClass<*>.function()
l
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
why is that a code smell? Calling static methods by way of an instance is the code smell
l
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
expose in what sense?
l
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
Are you trying to call extension methods from Java?
k
@Shawn I may be misunderstanding but I think you missed that he wants static extension functions.
s
Ahh, as though they were defined as a static method within the class body
l
@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
that was my bad
l
All good, technical problems are hard to communicate 🙂
👍 1
Is this even possible? I am not sure it is
s
Unfortunately I don’t think it is 😕
k
No, same for Kotlin classes that don't define a companion themselves.
l
That's unfortunate. Oh well, thanks guys!
g
Please vote for this feature and share your use cases https://youtrack.jetbrains.com/issue/KT-11968
👏 1