elect
02/27/2023, 6:19 PMvalue: Value<N : Number>
and I want temporarily to cast is as Value<N : Number, N: Comparable<N>
?Laertes Moustakas
02/27/2023, 6:31 PMfun whatever(value: Value<N>): T
where N: Number,
N: Comparable<N>
Youssef Shoaib [MOD]
02/27/2023, 7:20 PMvalue as Value<Comparable<N>>
not work? If not, maybe this thread from long ago about intersection types could help?
I'm thinking something along the lines of:
sealed interface TypeWrapper<out T> {
companion object: TypeWrapper<Nothing>
}
@Suppress("BOUNDS_NOT_ALLOWED_IF_BOUNDED_BY_TYPE_PARAMETER")
@OptIn(ExperimentalContracts::class)
// R is automatically inferred to just be T1 & T2.
// the callsite doesn't have to supply a type for R since you can't explicitly write out intersection types.
fun <T1, T2, R> intersection(): TypeWrapper<R>
where R : T1, R : T2
= TypeWrapper
inline fun <reified T> Any.cast(type: TypeWrapper<T>): T = this as T
fun main() {
val value: Any = 5
println(value.cast(intersection<Number, Comparable<*>, _>()).toDouble())
}
I think you can edit that and perhaps make another function that takes a TypeWrapper<T>
and outputs a TypeWrapper<Value<T>>
and use that for the cast.
Let me know if that works, and don't hesitate to ask any questions!elect
02/27/2023, 7:54 PMYoussef Shoaib [MOD]
02/27/2023, 7:56 PMValue
class look like btw?elect
02/27/2023, 7:59 PMclass SliderRangeVars {
inner class Value<N> where N : Number, N : Comparable<N> {
lateinit var cur: N
lateinit var min: N
lateinit var max: N
var resultFlags = 0
}
fun <N> values(): Array<Value<*>> = arrayOf(Value<Byte>(), Value<Ubyte>())
}
I also tried experimenting moving N
on the outer class, but I get other problems later on..
I can add you to the repo, if you wantYoussef Shoaib [MOD]
02/27/2023, 8:17 PMValue<N> where N: Number
without also adding the Comparable boundary. Please do add me to the repo and I'll tinker and see what I can do. My github is @kyay10