Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

375
Views
Kotlin use UInt for array access and constant

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

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

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.

over 4 years ago · Santiago Trujillo Report

0

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.)

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!