unsigned data types could be nice for array access. Usually the indexes are unsigned anyway. But currently I cannot directly do this. E.g. this code.
val foo = 1.toUInt()
"foo"[foo]
fails to compile with:
error: type mismatch: inferred type is UInt but Int was expected
what is the best way to deal with this? Sure I could do:
val foo = 1.toUInt()
"foo"[foo.toInt()]
but this feels wrong somehow. UInt is an inline class anyway and gets erased to Int anyway - so I think this should not be needed. Anyone saw a kotlin/KEEP for this? Also wondering about how to define unsigned constants. Unfortunately the constructor is private so I cannot do e.g.
const val foo = UInt(42)
and
const val foo = 42.toUInt()
fails with 42.toUInt() is not a constant value
In the array indexing question, .toInt() is the best method I have found.
Declaring a const, you can append a "u" to any integer constant, or "uL" for a long constant, like 42u or 1_000_000_000_000uL.
Unless/until there's built-in support for this, you can easily add it yourself. For example, for standard arrays:
operator fun <T> Array<T>.get(index: UInt) = this[index.toInt()]
And for CharSequences (which aren't arrays):
operator fun CharSequence.get(index: UInt) = this[index.toInt()]
With that in scope, your "foo"[foo] works fine!
(You'd also need separate overloads for IntArray &c if you used those.)