I have a multiplication operator override in my base class
open class Tuple(
open val x: Double,
open val y: Double,
open val z: Double,
open val w: Double
) {
open operator fun times(scalar: Double) = Tuple(x * scalar, y * scalar, z * scalar, w)
}
I would like for this operator override to work exactly the same for subclasses
data class Point(
override val x: Double,
override val y: Double,
override val z: Double
) : Tuple(x, y, z, 1.0) {
//Attempt 1
override operator fun times(scalar: Double) = Point(x * scalar, y * scalar, z * scalar)
// Attempt 2
private fun toPoint(other: Tuple) = Point(other.x, other.y, other.z)
operator fun times(scalar: Double) = toPoint(super.times(scalar))
}
but to instead return the subclass, which requires a subset of parameters of the base class in order to be instantiated.
Is there a way where I can write just one function and have it return the corresponding type based on the input class type?