Call a default constructor from another one in Kotlin

Viewed 12659

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)
1 Answers
Related