Hi, is it possible to add an external secondary co...
# getting-started
a
Hi, is it possible to add an external secondary constructor for a class ? I have a
typealias AABB = Rectangle
with an external "constructor"
fun Rectangle(x: Int = 0, y: Int = 0, width: Int = 0, height = 0) = ...
but it's not present on the type AABB, I also tested with
fun Rectangle.invoke(x: Int ...
but it still doesn't work, is it possible ?
c
If the class has a companion object, then you can make an extension on the companion which looks like a constructor:
fun Rectangle.Companion.invoke(...): Rectangle
r
You can just make a to level
fun Rectangle(...)
(or
fun AABB(...)
)
💯 2
a
Oh yes the
invoke
has to be on the Companion object, it kinda works, as I can't directly do
val a = AABB()
but
val a = AABB.invoke()
I just forgot
operator
, it works thanks x)
t
fun
with similar name, suggested by @Ruckus is more flexible solution in this case