The Kotlin1.5 Release Candidate is out: <https://b...
# feed
s
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
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
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
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
@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
I am so happy to see Char.digitToInt(). Now I don’t have to implement it myself for Advent of Code 2021. 😁
m
I did last year’s AoC with TypeScript … will definitely do it with Kotlin this year. 🙂
👍 1
r
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
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
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
Especially when doing various programming contests I never ever needed unsigned ints of any kind.
m
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
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
Thanks for the suggestion! There’s also codewars.com 🙂
t
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
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
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