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()=""
}
}
On
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.
There are multiple things at play here:
objectthat 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):
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:
Or directly if you import the function from the companion:
Such pattern is used for instance for
Duration.Compaionto 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