https://kotlinlang.org logo
Title
l

Luis Munoz

08/08/2019, 3:48 PM
s

Shawn

08/08/2019, 3:51 PM
unfortunately, secondary constructors need to call the primary as part of the constructor signature
I think you can kinda hack what you’re doing like this:
data class Example (
  val myName : String = "ellis"
) {
  constructor(notDefault: Boolean) : this(if (notDefault) "not ellis" else "ellis")
}
but that’s definitely kind of an odd usage, would probably surprise most folks looking at the code
l

Luis Munoz

08/08/2019, 3:53 PM
thanks was thinking the above
s

Shawn

08/08/2019, 3:54 PM
what’s usually prescribed in this situation is a factory method in the companion object, optionally implementing
operator fun invoke()
to mimic a constructor call
👍 1
l

Luis Munoz

08/08/2019, 3:54 PM
yours is better for being immutable