What is the point of naming a companion object in

2019-04-03 23:03发布

The documentation for companion objects has the following example

class MyClass {
    companion object Factory {
        fun create(): MyClass = MyClass()
    }
}

Here Factory is the name of the companion object. It then goes on to say:

The name of the companion object can be omitted, in which case the name Companion will be used:

However there is no example that I can see that uses the name of the companion object.

Since you can only have one companion object per class (otherwise you get a Only one companion object is allowed per class error) the name feels like some pretty useless syntactic sugar to me.

What can the name of the companion object actually be used for? Why would one bother to use any name for it?

3条回答
smile是对你的礼貌
2楼-- · 2019-04-03 23:50

You can use the name of the companion like:

MyClass.create()           // not via companion name
MyClass.Companion.create() // via default companion name
MyClass.Factory.create()   // via companion name

The name is maybe not that important for Kotlin, because you can just access the method without knowing that there is a companion object (line one above). It is more like a personal style, if you want to make the access to such functions more explicit.

But for java interop it makes a difference, because you have to access the function via the companion name:

    MyClass.Factory.create();   // with named companion
    MyClass.Companion.create(); // with unnamed comanion
查看更多
欢心
3楼-- · 2019-04-03 23:50

If you do not use an explicit name, the companions name is Companion, thus it can be omitted, like you already quoted.

Sometimes you may want to have an explicit name in your calls, which would be MyClass.Factory.create() in your example. For namespace reasons maybe.

I don't see a many reasons to name a companion object, either. Except if you care about Java interop with your Kotlin code. Then, you need to explicitly write the companions name.

Another reason you might care about the name is, when you define an extension function on it:

  fun MyClass.Companion.ext() = "myext"

In this case, it can be clearer when it has a name like Factory, on which specific factory methods are added via extension.

查看更多
ゆ 、 Hurt°
4楼-- · 2019-04-04 00:01

However there is no example that I can see that uses the name of the companion object.

class Person(val name: String) { companion object Loader {
fun fromJSON(jsonText: String): Person = ... }
}
>>> person = Person.Loader.fromJSON("{name: 'Dmitry'}") >>> person.name
Dmitry
>>> person2 = Person.fromJSON("{name: 'Brent'}") >>> person2.name
Brent
查看更多
登录 后发表回答