Call a default constructor from another one in Kotlin

apouche picture apouche · Mar 14, 2016 · Viewed 9.8k times · Source

In Kotlin, when a class has multiple constructors how can we call the designated (coming from iOS world I could not find a better name) constructor from within another constructor.

Let me show you an example

final class LoadingButton: LinearLayout {
    var text:String? = null

    constructor(context: Context, text: String) : this(context) {
        this.text = text
    }
    constructor(context: Context) : super(context) {
        val t = this.text
        this.view {
            button(t) { /* ... */}
        }
    }
}

Here if I do loadingButton("TEST", {}) that string is not propagated to the button because this(context) is called before the code inside the convenience constructor (sorry again :).

Can that be solved in Kotlin ? Something like

constructor(context: Context, text: String) {
    this.text = text
    this(context) 
}

EDIT

Just to clarify the idea since it's been asked, the idea is to write something like this in an activity :

onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)

    verticalLayout {
        loadingButton("Some text")
        //or
        loadingButton() { this.text = "Some text"}
}

This is obviously not that much useful but you get the idea. The text property can be known at construction or later on.

This is especially not useful since Kotlin has default values for parameters but I am studying the language and ran into this problem.

EDIT 2

Another clarification is that I'm using Anko for the layout, so the loadingButton methods look like this:

inline fun ViewManager.loadingButton() = loadingButton {  }
inline fun ViewManager.loadingButton(init: LoadingButton.() -> Unit) = ankoView({ LoadingButton(it) }, init)
inline fun ViewManager.loadingButton(text: String, init: LoadingButton.() -> Unit) = ankoView({ LoadingButton(it, text) }, init)

Answer

voddan picture voddan · Mar 14, 2016

The code with a post-call to a constructor cannot exist on JVM since you MUST call super(...) before you do anything with the class itself. Think of it as if the super class contained a private field, you would have to initialise it before you can use it.

This is usually not a problem since you can call the constructors the other way around:

constructor(context: Context, text: String?) : super(context) {
    this.text = text
    this.view {
        button(text) { /* ... */}
    }
}
constructor(context: Context) : this(context, null)
constructor(context: Context, text: String) : this(context, text)

The code above does roughly the same as a default argument:

constructor(context: Context, text: String? = null) : super(context) {
    this.text = text
    this.view {
        button(text) { /* ... */}
    }
}

To make this code idiomatic (and concise), use a primary constructor:

class LoadingButton(context: Context, val text: String? = null): LinearLayout(context) {
    init {
        this.view {
            button(text) { /* ... */}
        }
    }
}

The terminology goes as following: designated - primary, convenience - secondary

See Classes and Inheritance - Kotlin Programming Language for more details.