Proposal: Allow abstract constructors Motivation:...
# language-proposals
b
Proposal: Allow abstract constructors Motivation: Java allowed abstract methods, and Kotlin now offers abstract functions and fields. Since constructors are just specialized functions/methods, why not offer abstract constructors? Use case: With generic abstract classes, I want a more complex class to have its default constructor to take in one or more complex objects. However, I also want to provide a convenience constructor for the API user so they don't have to create objects in order to create objects. SSCCE:
Copy code
open class Point<NumberType: Number>(
    val x: NumberType,
    val y: NumberType
)

open class Size<NumberType: Number>(
    val width: NumberType,
    val y: NumberType
)

abstract class Rect<NumberType: Number, PointType: Point<NumberType>, SizeType: Size<NumberType>>(
    val origin: PointType,
    val size: SizeType
) {
    abstract constructor(x: NumberType, y: NumberType, width: NumberType, height: NumberType)  //  <== Constructor declared abstractly

    val x get() = this.origin.x
    val y get() = this.origin.y
    val width get() = this.size.width
    val height get() = this.size.height

    open fun copy(
        newOrigin: PointType = this.origin,
        newSize: SizeType = this.size
    ): Rect<NumberType, PointType, SizeType>
        = Rect(newOrigin, newSize)

    open fun copy(
        newX: NumberType = this.x,
        newY: NumberType = this.y,
        newWidth: NumberType = this.width,
        newHeight: NumberType = this.height
    ): Rect<NumberType, PointType, SizeType>
        = Rect(newX, newY, newWidth, newHeight)
}

class MyRect(origin: Point<Int>, size: Size<Int>): Rect<Int, Point<Int>, Size<Int>>(origin, size) {
    override constructor(x: Int, y: Int, width: Int, height: Int): this(Point(x, y), Size(x, y))  //  <== Constructor implemented/overridden
}
Full, non-SSCCE version: https://github.com/BlueHuskyStudios/Blue-Base/blob/865d1330402eeb6f5d2b505a0a148df2ea46c4e7/JVM/src/org/bh/tools/base/math/geometry/Rect.kt#L19