https://kotlinlang.org logo
#feed
Title
s

Stephan Schroeder

04/15/2021, 8:39 AM
With UInt being stable now, I wonder if they can be used for indexing in Arrays in the future. Well, all kinds of indexing actually including for Strings, like having an
fun String.indexOfOrNull(s: String): UInt?
including UInt versions of
substring
, etc.
m

Michael Böiers

04/15/2021, 8:49 AM
I was surprised that new features like the
upperCase
method are still annotated as experimental …
While the current methods (e.g.
toUpperCase
) are already annotated as deprecated.
👍 2
s

Stephan Schroeder

04/15/2021, 9:18 AM
are you sure that that's not simply your IDE using an older version of the language? In my case IntelliJ is flagging a lot of things, that actually running the code with gradle (and therefore the proper version of Kotlin) has no issues with.
With UInt being stable now, I wonder if they can be used for indexing in Arrays in the future. Well, all kinds of indexing actually including for Strings, like having an
fun String.indexOfOrNull(s: String): UInt?
including UInt versions of
substring
, etc.
I'm aware that I can simply add those myself as extension functions, at least for String though doing so for Array isn't optimal because
Copy code
operator fun <T> Array<T>.get(index: UInt): T {
    val indexAsInt = index.toInt()
    require(indexAsInt>=0) {"index isn't allowed to be bigger than Int.MAX_VALUE ($Int.MAX_VALUE}), but is: $index"}
    return this[indexAsInt]
}
has two issues: • the implementation will still finally check if the index is at least 0 • I'm still limited by Int.MAX_VALUE as Array length
c

Colton Idle

04/15/2021, 9:33 AM
I've never felt the need to use an Int that can't be negative (UInt?) Can someone point out a scenario where that would be needed?
m

Michael Böiers

04/15/2021, 9:45 AM
@Colton Idle adventofcode.com 🙂
🤔 1
In my professional work I’ve never come across a problem that required unsigned numbers. The only thing I can think of is indeed some assignments in last year’s advent of code challenge. I guess that is largely due to the fact that I never did much scientific/mathematical work.
👍 1
t

todd.ginsberg

04/15/2021, 1:07 PM
I am so happy to see Char.digitToInt(). Now I don’t have to implement it myself for Advent of Code 2021. 😁
m

Michael Böiers

04/15/2021, 1:39 PM
I did last year’s AoC with TypeScript … will definitely do it with Kotlin this year. 🙂
👍 1
r

rocketraman

04/15/2021, 4:47 PM
The possibility to store a bigger number in the same number of bytes without overflow i.e.
Int.MAX_VALUE
is much smaller than
UInt.MAX_VALUE
.
e

elizarov

04/15/2021, 5:21 PM
Use-cases for UInt mostly revolve around native lib interop, various byte-oriented protocols/formats, and (extremely rarely) for custom optimized data structures. They have little use elsewhere, definitely not worth using in the general application code.
👍 7
m

Michael Böiers

04/15/2021, 8:46 PM
Here’s the challenge from AoC that involved unsigned integers. Not so easy to solve using signed ints (like plain JS numbers) … https://adventofcode.com/2020/day/14
e

elizarov

04/16/2021, 5:50 AM
Especially when doing various programming contests I never ever needed unsigned ints of any kind.
m

Michael Böiers

04/16/2021, 6:34 AM
Here’s my solution, which isn’t clean and was arrived at between changing my daughter’s diapers and moving from Germany to Sweden in december. 🙂 https://github.com/MikeEnRegalia/AdventOfCode2020/blob/master/Day14.ts
I haven’t done all the AoCs - now that I “speak” Kotlin, I might do one of the previous years. Should be fun!
e

elizarov

04/16/2021, 6:39 AM
If you are into this kind of programming, I do recommend to take a look at codeforces.com Their Div.3 contests are of the same complexity as AoC, but happen regularly, and there is a room to grow into higher ranks.
m

Michael Böiers

04/16/2021, 6:48 AM
Thanks for the suggestion! There’s also codewars.com 🙂
t

todd.ginsberg

04/17/2021, 4:07 PM
Oh cool, I had no idea you had done Advent of Code 2020 Roman! 🙂 Now I've got something to read and learn from this weekend. I solved 2020 d14 without UInt as well, FWIW.
i

ianbrandt

04/20/2021, 5:15 PM
We work with network protocols that utilize unsigned types heavily, so I very much appreciate the unsigned type support in Kotlin. I could also see unsigned types being neat for things like delay(timeMillis) and withTimeout(timeMillis), which I'm just now realizing lack documentation on how negative values are handled. `lateinit` support for unsigned types would be nice, as would bitwise operator precedence.
e

elizarov

04/26/2021, 12:18 PM
Unsigned types are not designed for "negative value excluded" cases and we don't plan to use them in functions like
delay
. Unsigned types are designed for storage of values that need to utilize all the available bits for non-negative values.
💯 2
2 Views