Michał Kalinowski
12/30/2019, 3:28 PMclass Angle<T>
where T can be Int or Float or Double and nothing elseDominaezzz
12/30/2019, 4:31 PMclass Angle<T : Number>
is about as good as it gets.Matteo Mirk
12/30/2019, 4:41 PMclass Angle<T : Number>
but that will accept also Long, Short and Byte.
If you want to restrict to those types only I don’t see any way other than creating your specific type hierarchy:
class Angle<T : Aunit>(value: T)
interface Aunit
inline class AInt(val value: Int) : Aunit
inline class ADouble(val value: Double) : Aunit
fun main() {
Angle(AInt(1))
Angle(ADouble(1.0))
Angle(1) // obviously doesn't compile
}
Otherwise, if you don’t care about compile-time safety you could get away with runtime checks:
class Angle<T : Number>(value: T) {
init {
require(value is Int || value is Float || value is Double)
}
}
Michał Kalinowski
12/30/2019, 4:49 PMMatteo Mirk
12/30/2019, 4:52 PMclass Angle(val v: Double) {
constructor(v: Float) : this(v.toDouble())
constructor(v: Int) : this(v.toDouble())
}
fun main() {
Angle(1)
Angle(1.0)
Angle(1.0f)
Angle(1L) // doesn't compile
}
the Idea is that your Angle will hold a Double property but you can initialize it also with an int or floatMichał Kalinowski
12/30/2019, 5:02 PMsealed class Angle{
abstract val value: Double
}
data class RadiansAngle(override val value: Double) : Angle() {
constructor(angle: DegreesAngle) : this(angle.toRadians().value)
}
data class DegreesAngle(override val value: Double) : Angle() {
constructor(angle: RadiansAngle) : this(angle.toDegrees().value)
}
Matteo Mirk
12/30/2019, 5:18 PMsealed class Angle
data class RadiansAngle(val value: Double) : Angle() { }
data class DegreesAngle(val value: Double) : Angle() { }
also in my opinion you don’t need conversion constructors from one type to another, just keep the to*()
methods. I would prefer to use radians.toDegrees()
rather than DegreesAngle(radians)
😉Michał Kalinowski
12/30/2019, 5:37 PMIvann Ruiz
12/30/2019, 7:31 PMclass Angle<T : Union<Int, Union<Float, Double>>>
Ivann Ruiz
12/30/2019, 7:33 PM