How do I get this to work?
# announcements
l
s
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:
Copy code
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
Untitled
thanks was thinking the above
s
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
yours is better for being immutable