Reputation: 9123
My class:
class Manager (var name: String, var nationality: String) {
constructor(agent: String): this() {}
}
returns the following error:
None of the following functions can be called with the arguments supplied.
<init>(String) defined in Manager
<init>(String, String) defined in Manager
Any idea why?
Upvotes: 3
Views: 6220
Reputation: 97120
Your class has a primary constructor that takes two arguments, and then you define a secondary constructor that takes one argument.
Now, as per the Kotlin documentation:
If the class has a primary constructor, each secondary constructor needs to delegate to the primary constructor, either directly or indirectly through another secondary constructor(s).
You're trying to do that by calling this()
, but since you don't have a zero-argument constructor (primary or secondary), this results in a compilation error.
To fix, for example, you can call your primary constructor from your secondary constructor as follows:
class Manager (var name: String, var nationality: String) {
constructor(agent: String): this(agent, "") {}
}
Upvotes: 4