elect
11/23/2021, 8:49 AMNumber
). So I was thinking, maybe we can have inline classes exposes/extends/delegates specific interfaces/classes from the underlying value..
something like
inline class UInt(val i: Int) : Number by i
ephemient
11/23/2021, 8:54 AMelect
11/23/2021, 8:54 AMephemient
11/23/2021, 12:02 PMval foo: Number = 1u
then foo
will be a real object of type kotlin.UInt
, "boxed" just like val foo: Number = 1
will be a boxed Integer, not a primitive. there's very few reasons to use Numberelect
11/23/2021, 12:04 PMNumber
for base type such as thisephemient
11/23/2021, 12:05 PMelect
11/23/2021, 12:05 PMelect
11/23/2021, 12:05 PMephemient
11/23/2021, 12:05 PMephemient
11/23/2021, 12:10 PMIntArray
, UIntArray
, etc.elect
11/23/2021, 12:10 PMephemient
11/23/2021, 12:12 PMIntPair
, UIntPair
, etc.elect
11/23/2021, 4:46 PMNumber
constraint means I cannot longer use these for unsignedelect
11/23/2021, 4:48 PMephemient
11/23/2021, 4:50 PMelect
11/23/2021, 4:50 PMjimn
12/02/2021, 6:47 PMinterface BitOps<Primitive : Comparable<Primitive>> {
val one: Primitive
val shl: (Primitive, Int) -> Primitive
val shr: (Primitive, Int) -> Primitive
val xor: (Primitive, Primitive) -> Primitive
val and: (Primitive, Primitive) -> Primitive
val or: (Primitive, Primitive) -> Primitive
val plus: (Primitive, Primitive) -> Primitive
val minus: (Primitive, Primitive) -> Primitive
fun toNumber(x: Primitive): Number = x.let {
when (one) {
is Number -> it as Number
is UByte -> (it as UByte).toInt()
is UShort -> (it as UShort).toInt()
is UInt -> (it as UInt).toLong()
else -> (it).toString().toBigInteger()
}
}
companion object {
/**
* minimum bitops types for the intended bitcount of NUID
*/
fun minOps(size: Int): BitOps<*> = when (size) {
in Int.MIN_VALUE..7 -> ByteBitOps
8 -> UByteBitOps
in 9..15 -> ShortBitOps
16 -> UShortBitOps
in 17..31 -> IntBitOps
32 -> UIntBitOps
in 33..63 -> LongBitOps
64 -> ULongBitOps
else -> BigIntOps
}
}
}
some amount of credit to @ephemient Numeric as well but no overlapjimn
12/02/2021, 7:05 PM