In Kotlin language, what does this syntax do and how does it work ?
class ClassName1 {
companion object {
fun ClassName2.funName()=""
}
}
In Kotlin language, what does this syntax do and how does it work ?
class ClassName1 {
companion object {
fun ClassName2.funName()=""
}
}
There are multiple things at play here:
object
that is associated to a class and can be referenced by just using the class nameDeclaring a member extension function inside a companion object might be useful for different things.
For instance, such function can be used within the class as if the extension function was declared as a class member but outside the companion. Some people prefer to put them in the companion object to make it clear that they don't depend on the state of said class (like a Java static function):
class ClassName1 {
fun method(): String {
val something = ClassName2()
return something.funName()
}
companion object {
fun ClassName2.funName() = ""
}
}
Such use doesn't require the function to be public, though.
Another way to use this would be by using the companion object as a sort of scope:
val something = ClassName2()
with(ClassName1) { // this: ClassName1.Companion
something.funName() // brought in scope by ClassName1's companion
}
Or directly if you import the function from the companion:
import ClassName1.Companion.funName
val something = ClassName2()
something.funName()
Such pattern is used for instance for Duration.Compaion
to define extensions on number types (those are extension properties but it's the same idea):
https://github.com/JetBrains/kotlin/blob/6a670dc5f38fc73eb01d754d8f7c158ae0176ceb/libraries/stdlib/src/kotlin/time/Duration.kt#L71
funName()
is called by method()
in the first example. Did you miss it or are you looking for something else? –
Womble ClassName1
depends on whether the visibility allows it. And of course if the visibility allows it you still need to import it. When the visibility is private
, there is still a difference between declaring in the class or in the companion. When it's in the companion, the body of the function doesn't have access to class members, which might be desirable sometimes to express that the function is pure –
Womble This is a weird syntax. One example to use this can be:
import ClassName1.Companion.funName
class ClassName1 {
companion object {
fun ClassName2.funName() = ""
}
}
class ClassName2
fun main() {
ClassName2().funName()
}
Here I had to import funName
from Class1.Companion
in order to call it. There's no easy way to call this function.
If you don't have past experience with extension functions, you can take a look at the decompiled bytecode to see what's happening under the hood:
public final class ClassName2 {
}
public final class ClassName1 {
@NotNull
public static final ClassName1.Companion Companion = new ClassName1.Companion((DefaultConstructorMarker)null);
public static final class Companion {
@NotNull
public final String funName(@NotNull ClassName2 $this$funName) {
return "";
}
private Companion() {
}
public Companion(DefaultConstructorMarker $constructor_marker) {
this();
}
}
}
public final class YourFileNameKt {
public static final void main() {
ClassName1.Companion.funName(new ClassName2());
}
}
funName
is a function inside static Companion
class declared inside ClassName1
. It receives a ClassName2
object as a parameter (this is how extension functions work, nothing special here).
But I would say that this type of declaration is very confusing. It would be better if you could provide more info on how this is being used in your case. Passing a ClassName2
object directly in the function seems to be a much cleaner approach here.
© 2022 - 2024 — McMap. All rights reserved.