Nick
04/08/2020, 3:12 AMRuckus
04/08/2020, 3:30 AMNick
04/08/2020, 3:50 AMDavid Eriksson
04/08/2020, 10:35 AMstreetsofboston
04/08/2020, 12:41 PMRuckus
04/08/2020, 2:05 PMN
and m
would still need to be associated with either A
or B
.Nick
04/08/2020, 7:11 PMnkiesel
04/08/2020, 8:19 PMfun foo(a: Int, b: String)
, one could argue that I should be allowed to invoke that with foo("hello", 42)
. And the language actually would support that by using foo(b = "Hello", a = 42)
. So, are you looking for something similar but for type parameters?streetsofboston
04/08/2020, 10:13 PMinterface Unit {
val value: Double
}
interface Magnitude<U1: Unit, U2: Unit> {
val value: Double
}
interface MagnitudeFactory<U1: Unit, U2: Unit, M: Magnitude<U1, U2>> {
operator fun invoke(value1: Unit, value2: Unit): M
}
/* ====== */
class Force(override val value: Double): Unit
class Length(override val value: Double): Unit
class Torque(override val value: Double): Magnitude<Force, Length> {
companion object: MagnitudeFactory<Force, Length, Torque> {
override operator fun invoke(value1: Unit, value2: Unit) =
Torque(value1.value * value2.value)
}
}
val Double.Nm: Torque get() = Torque(this)
val <http://Double.mN|Double.mN>: Torque get() = Torque(this)
operator fun Force.times(length: Length): Torque = Torque(this, length)
operator fun Length.times(force: Force): Torque = Torque(this, force)
/* ====== */
fun test() {
val t1 = 10.4.Nm
val t2 = 23.4.mN
val t3 = Force(3.1) * Length(4.0)
val t4 = Length(4.0) * Force(3.1)
}