Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

374
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda