dimanche 26 novembre 2017

Kotlin - any substitute for companion object inside of another object (not class)?

I would like in my Kotlin Android app to have singleton object with some static definitions of it's inner states.

As I understand, object in Kotlin is for singleton, so I am trying this kind of approach:

object MySingleton
{
    public const val _DEF_DEFINITION_NO_ONE: Byte = 1;
    public const val _DEF_DEFINITION_NO_TWO: Byte = 2;
    (...)
}

This is fine, but the problem is, to being able use these definitions, now I must create Object's instance first.

Just wondering if I am able in Kotlin to create this kind of construction and have access to these definitions without creating MySingleton instance? Answer would be companion object working similar as static in other languages, but it's not allowed inside objects, only inside of classes.

Of course I can leave this as is or make these definitions global, but would like to know is it possible to do in a way I described? Or maybe should I design this in another yet way?

Aucun commentaire:

Enregistrer un commentaire