Calling a class's method as default arg in constructor

Tom

I'm constructing a class and then trying to call a member method of that class as a default value for one of the constructor args.

Why isn't this valid Kotlin?

// unresolved reference: defaultText
class MyThing(val text: String = defaultText()) {  
    fun defaultText() = "hi"
}

It's possible using two separate constructors in both Java and Kotlin, but then I lose the conciseness of default args.

class MyThing {

    private val text: String

    constructor(text: String) {
        this.text = text
    }

    constructor() {
        this.text = defaultText()
    }

    private fun defaultText(): String {
        return "hi"
    }
}
hotkey

The biggest problem of having a constructor's default parameter expression call a member function of the same instance is that the default arguments are evaluated before the constructor is called.

Given that, such a member function would have to run on a completely un-initialized instance of the class (because even the super constructors will work after that, see this answer about the execution order).

Usually, member functions perform some logic taking the instance state into account, and having a member function run on an empty instance might break some of that logic (e.g. all fields will hold nulls, even the backing fields of Kotlin not-null properties). Overall, even when such calls do not fail at runtime, they are likely introduce subtle bugs, so using a completely uninitialized instance is prohibited.

With regard to the secondary constructor, well, at least it runs after the super constructor initializes some part of the instance, which is thus not completely empty, but it's up to you to make sure you don't use the parts of the class that are not initialized (if you do, you may again encounter a runtime failure or introduce a bug).

I'd rather suggest using a function of a companion object (those are initialized before the class is first used) for this purpose:

class MyThing(val text: String = defaultText()) {  
    companion object {
        fun defaultText() = "hi"
    }
}

Or even a top-level function:

fun defaultText() = "hi"

class MyThing(val text: String = defaultText())

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Javascript

Calling a class method from the constructor

From Dev

Member's destructor calling from default constructor

From Dev

Calling a constructor from method within the same class

From Java

Calling virtual method in base class constructor

From Dev

Unity - Inject an object to Constructor by calling a Method of a Class

From Java

Calling a method with an arg of Class<T> where T is a parameterized type

From Dev

Calling a default method from Interface in Main Class

From Dev

Calling a overridable method in a constructor, even if we specify it's from the super class?

From Java

CustomDeserializer has no default (no arg) constructor

From Dev

Applying constructor's single arg interface as properties on the class

From Dev

Calling a method on a constructor method

From Dev

Calling constructor of parent class in static method of child class

From Dev

Calling ES6 class constructor from class static method

From Dev

Calling class constructor later in another class method in C++

From Dev

Is calling a superinterface's default method possible?

From Dev

Spring not calling the default constructor

From Dev

Calling the default constructor in scala

From Dev

program calling default constructor?

From

What's the point of deleting default class constructor?

From Dev

compiler warning with reflection calling empty arg constructor

From Dev

Calling a constructor with default parameters instead of default constructor

From Java

Is method reference calling constructor of ArrayNode class under the hood?

From Dev

Javascript - Class - Method Returning Undefined When Calling Constructor Property

From Dev

Calling a void Method into a constructor

From Java

Calling method from constructor

From Dev

Calling a constructor as a method

From Dev

Calling Constructor with in constructor in same class

From Dev

Derived class's method without constructor

From Dev

Calling component's method from class

Related Related

  1. 1

    Calling a class method from the constructor

  2. 2

    Member's destructor calling from default constructor

  3. 3

    Calling a constructor from method within the same class

  4. 4

    Calling virtual method in base class constructor

  5. 5

    Unity - Inject an object to Constructor by calling a Method of a Class

  6. 6

    Calling a method with an arg of Class<T> where T is a parameterized type

  7. 7

    Calling a default method from Interface in Main Class

  8. 8

    Calling a overridable method in a constructor, even if we specify it's from the super class?

  9. 9

    CustomDeserializer has no default (no arg) constructor

  10. 10

    Applying constructor's single arg interface as properties on the class

  11. 11

    Calling a method on a constructor method

  12. 12

    Calling constructor of parent class in static method of child class

  13. 13

    Calling ES6 class constructor from class static method

  14. 14

    Calling class constructor later in another class method in C++

  15. 15

    Is calling a superinterface's default method possible?

  16. 16

    Spring not calling the default constructor

  17. 17

    Calling the default constructor in scala

  18. 18

    program calling default constructor?

  19. 19

    What's the point of deleting default class constructor?

  20. 20

    compiler warning with reflection calling empty arg constructor

  21. 21

    Calling a constructor with default parameters instead of default constructor

  22. 22

    Is method reference calling constructor of ArrayNode class under the hood?

  23. 23

    Javascript - Class - Method Returning Undefined When Calling Constructor Property

  24. 24

    Calling a void Method into a constructor

  25. 25

    Calling method from constructor

  26. 26

    Calling a constructor as a method

  27. 27

    Calling Constructor with in constructor in same class

  28. 28

    Derived class's method without constructor

  29. 29

    Calling component's method from class

HotTag

Archive