I’m not sure `return` makes sense in a `constructo...
# language-proposals
b
I’m not sure
return
makes sense in a `constructor`…
e
if some conditions arent met, then avoid going on
b
In Kotlin, a
constructor
always creates a new instance. If you look above, I suggested a “failable” constructor, which might return
null
if some conditions aren’t met. It was pointed out to me that placing a
operator fun invoke(
inside the class’s
companion object
gives the same effect. You should try that!
d
You can just introduce a function with the same name:
Copy code
class C(val x: Int)
fun C(xs: List<Int>) = x.singleOrNull()?.let { C(it) }
Can't say I like any of these styles, though
b
@dmitry.petrov what styles do you mean? Like, how to make the fancy initializer, or the concept of one in the first place?
d
I mean factory function that masquerades as a constructor. But probably I'm somewhat old-fashioned.
😛 1