Stephan Schroeder

    Stephan Schroeder

    1 year ago
    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.
    Michael Böiers

    Michael Böiers

    1 year ago
    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.
    Stephan Schroeder

    Stephan Schroeder

    1 year ago
    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
    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
    Colton Idle

    Colton Idle

    1 year ago
    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?
    Michael Böiers

    Michael Böiers

    1 year ago
    @Colton Idle adventofcode.com 🙂
    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.
    todd.ginsberg

    todd.ginsberg

    1 year ago
    I am so happy to see Char.digitToInt(). Now I don’t have to implement it myself for Advent of Code 2021. 😁
    Michael Böiers

    Michael Böiers

    1 year ago
    I did last year’s AoC with TypeScript … will definitely do it with Kotlin this year. 🙂
    r

    rocketraman

    1 year ago
    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
    .
    elizarov

    elizarov

    1 year ago
    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.
    Michael Böiers

    Michael Böiers

    1 year ago
    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
    elizarov

    elizarov

    1 year ago
    Especially when doing various programming contests I never ever needed unsigned ints of any kind.
    Michael Böiers

    Michael Böiers

    1 year ago
    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!
    elizarov

    elizarov

    1 year ago
    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.
    Michael Böiers

    Michael Böiers

    1 year ago
    Thanks for the suggestion! There’s also codewars.com 🙂
    todd.ginsberg

    todd.ginsberg

    1 year ago
    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

    1 year ago
    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.
    elizarov

    elizarov

    1 year ago
    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.