https://kotlinlang.org logo
Join Slack
Powered by
# stdlib
  • d

    diego-gomez-olvera

    02/18/2025, 11:53 AM
    hello, everyone! I am curious to know when Base64 will stop being a
    @ExperimentalEncodingApi
    . I created some time ago a wrapper for android.util.Base64 which uses Java Base 64 for JVM tests and I wonder if I could fully replace it with the Kotlin version for KMP compatibility without having to change it later
    f
    • 2
    • 2
  • p

    PHondogo

    02/19/2025, 6:20 PM
    Is there any resource where i can watch for stdlib release notes history?
    f
    • 2
    • 1
  • r

    Rohde Fischer

    02/21/2025, 10:05 AM
    is it possible to design a recursive default function on an interface to operate on the super type until it "hits" itself? I'm thinking something like this (semi-pseudocode):
    Copy code
    interface Foo {
        val selfValue: String
    
        fun doSomething(): String {
            if (this::class == Foo::class) {
                return "Foo"
            } else {
                return "${super.doSomething()}.$selfValue"
            }
        }
    }
    j
    c
    • 3
    • 3
  • k

    Klitos Kyriacou

    02/21/2025, 1:56 PM
    The Java API has a constructor that allows you to pass an initial String or CharSequence to initialize it with a sensible initial capacity, e.g.:
    Copy code
    val str = StringBuilder("The value of your purchase is ").apply {
        ...do things...
        append(things)
    }.toString()
    This would be neater if
    buildString
    had an overload that takes a
    String
    or
    CharSequence
    parameter. Without it, we would have to do the above, or the even worse:
    Copy code
    val str = buildString("The value of your purchase is ".length + 16) {
        append("The value of your purchase is ")
        ...do things...
        append(things)
    }
    Has anyone ever made a request for such a function and what was the outcome?
    j
    • 2
    • 2
  • g

    George

    03/04/2025, 4:30 PM
    Hello folks, I am trying to benchmark the sequence API vs the stream API vs simple transformations. I am seeing some weird results that the stream API is always the slowest no matter the scenario (even for short circuit). Here is the repo: https://github.com/GeorgePap-719/StreamAllocationBenchmark
    f
    h
    • 3
    • 6
  • c

    CLOVIS

    03/22/2025, 10:11 AM
    Could we get a
    TreeMap
    in the standard library?
    ➕ 2
    e
    • 2
    • 6
  • j

    Javier

    03/23/2025, 1:27 PM
    With the new atomic types in the stdlib, is it still necessary to apply the atomicfu plugin? I guess the one from
    kotlin
    and not the one from
    Kotlinx
    is still necessary.
    y
    f
    • 3
    • 2
  • k

    Klitos Kyriacou

    04/02/2025, 12:04 PM
    We have a sum function for all the primitive types, but not for `BigDecimal`:
    Copy code
    listOf(1, 2, 3).sum()
    listOf(1.toBigDecimal(), 2.toBigDecimal()).sum()  // Error
    listOf(1.toBigDecimal(), 2.toBigDecimal()).sumOf { it }  // OK
    Since there is already a
    sumOf
    function specialized for
    BigDecimal
    , why isn't there a
    sum
    function for it too?
    ➕ 4
    h
    g
    • 3
    • 2
  • g

    ghedeon

    04/19/2025, 5:22 PM
    Is my guess correct, that there is no left/right versions of binary search in kotlin that can deal with duplicates, the way it's supported in python (
    bisect_left
    ,
    bisect_right
    )? Sad java Collections legacy
    k
    • 2
    • 1
  • c

    CLOVIS

    04/19/2025, 5:45 PM
    I'm surprised there isn't a
    IntRange.size
    … Am I missing something?
    h
    h
    • 3
    • 2
  • e

    Eugen Martynov

    04/20/2025, 12:26 PM
    Wonder why standard library doesn’t have binarySearch variant for arrays?
    c
    d
    k
    • 4
    • 9
  • v

    Vasily

    04/21/2025, 12:14 PM
    `Flow`s have a wonderful catch extension, and I wonder if a similar functionality could be added to `Sequence`s? The following implementation appears to work as expected:
    Copy code
    fun <T> Sequence<T>.catch(action: suspend SequenceScope<T>.(Throwable) -> Unit): Sequence<T> {
        return sequence {
           try {
              forEach { yield(it) }
           } catch (t: Throwable) {
              action(t)
           }
        }
    }
    Is the implementation correct? If so, could it be added to stdlib?
  • c

    CLOVIS

    04/25/2025, 9:58 PM
    The bitwise
    and
    or
    or
    operators are in package
    kotlin.experimental
    ? 🤔 https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.experimental/and.html
    t
    a
    j
    • 4
    • 4
  • l

    louiscad

    04/28/2025, 12:18 PM
    Hello, is there a reason we got new
    AtomicLong
    and friends in Kotlin 2.1.20, but still no KMP-compatible
    Lock
    ? I have a use case where I need to ensure a
    Map
    is edited with no concurrent modifications, and using
    kotlinx.coroutines.sync.Mutex
    has undesired consequences on the exposed API, and is also overkill.
    f
    • 2
    • 1
  • k

    Klitos Kyriacou

    04/28/2025, 5:21 PM
    I noted that in C#, the following LINQ code:
    Copy code
    veryLargeList.Order().Take(10).ToList();
    Does a Quickselect/partial Quicksort to avoid having to sort the entire array. Is there any chance that Kotlin Sequences can be optimized in the same way?
    Copy code
    veryLargeList.asSequence().sorted().take(10).toList()
    y
    k
    • 3
    • 4
  • l

    louiscad

    05/21/2025, 12:15 PM
    Hello! Is there an allocation-free, KMP equivalent of Java's
    System.nanoTime()
    ? I want to measure small things (at high frequency), without the measurement affecting the measurement too much.
    Clock.System.now()
    returns a newly allocated
    Instant
    , and
    TimeSource.Monotonic.markNow()
    returns a value class wrapping
    ValueTimeMarkReading
    , which I think is also not just a primitive but a full allocated object. And
    measureTime
    uses the latter function…
    👀 2
    ➕ 2
  • z

    Zoltan Demant

    05/30/2025, 2:15 PM
    Any way to make this work? 🙂
    Copy code
    package aggregate.common
    
    abstract class Keyed(
        val key: String,
    ) {
        // Does not compile, Id is value class that wraps String 
        constructor(id: Id) : this(
            key = id.value,
        )
    
        constructor(keyed: Keyed) : this(
            key = keyed.key,
        )
    }
    c
    p
    +2
    • 5
    • 28
  • c

    CLOVIS

    06/04/2025, 8:32 PM
    Are there plans to make
    Uuid
    and
    Instant
    @Serializable
    ?
    e
    h
    • 3
    • 9
  • e

    Eugen Martynov

    06/06/2025, 3:40 PM
    I wonder why we don't have
    Uuid.parseOrNull()
    c
    • 2
    • 1
  • s

    Slackbot

    06/06/2025, 4:25 PM
    This message was deleted.
    k
    e
    • 3
    • 2
  • z

    Zoltan Demant

    06/09/2025, 3:55 AM
    I keep getting warnings about
    timestampOffset
    always being 0 here, but its not the case. Compiler bug?
    Copy code
    var minTimestamp: Long? = null
    var maxTimestamp: Long? = null
    
    journal.trainings.fastForEach { training ->
        val timestamp = training.timestamp.millis
    
        minTimestamp = minTimestamp?.coerceAtMost(timestamp) ?: timestamp
        maxTimestamp = maxTimestamp?.coerceAtLeast(timestamp) ?: timestamp
    
        ..
    }
    
    val timestampOffset = minTimestamp ?: 0L
    val timeSpan = (maxTimestamp ?: timestampOffset) - timestampOffset // "Value of 'timestampOffset' is always zero"
    j
    d
    • 3
    • 2
  • z

    Zac Sweers

    06/09/2025, 1:03 PM
    Why doesn't
    sortedSetOf()
    return
    SortedSet
    ? Currently it returns
    TreeSet
    , which isn't wrong but feels like it's an unnecessary implementation detail exposure edit: found https://youtrack.jetbrains.com/issue/KT-20972
    👆 1
    👆🏻 1
  • d

    Derek Peirce

    06/13/2025, 5:46 AM
    Has it ever been considered to allow concatenation of `Regex`es that contain different options? A decent number of times, I've had a case where I want to build a regex that is composed mostly of constants, but with a lot of characters that would require escaping. For a very simplified example, it would be very handy if, instead of:
    Copy code
    Regex("""java\.lang\.ArrayIndexOutOfBoundsException: Index \d+ out of bounds for length \d+""")
    I could write:
    Copy code
    Regex("java.lang.ArrayIndexOutOfBoundsException: ", RegexOption.LITERAL) + Regex("""Index \d+ out of bounds for length \d+""")
    r
    e
    • 3
    • 3
  • r

    Raid Xu

    06/13/2025, 7:07 AM
    Hi, I'm wondering why
    ReentrantReadWriteLock.write
    in
    Locks.kt
    is marked as
    InlineOnly
    . Is it to forbid Java from calling it? But what's reason behind it?
    d
    d
    • 3
    • 2
  • m

    Marcin Wisniowski

    06/25/2025, 1:40 PM
    listOf(1, 2, 3).containsAll(listOf(2, 2))
    returns true. What's the best alternative that will tell me correctly that the first list doesn't, in fact, contain all the elements of the second list? I want to consider the amount of items.
    j
    k
    • 3
    • 11
  • e

    Eugen Martynov

    07/02/2025, 12:11 PM
    Why
    Long.toHexString()
    produces a different string comparing to Java one? There are extra
    0
    padding
    k
    • 2
    • 5
  • z

    Zoltan Demant

    07/03/2025, 11:27 AM
    Can someone explain to me how this is possible? I mean, it makes sense, A and B are different types, but theyre still value classes.
    Copy code
    sealed interface BranchIdentifier {
        val key: String
    }
    
    @JvmInline
    value class A(override val key: String) : BranchIdentifier
    
    @JvmInline
    value class B(override val key: String) : BranchIdentifier
    
    fun main() {
        val collection = mutableSetOf<BranchIdentifier>()
        collection.add(A("5"))
        collection.add(B("5"))
        println(collection.size) // Prints 2
        println(collection.contains(A("5"))) // true
        println(collection.contains(B("5"))) // true
        println(collection.remove(A("5"))) // true
        println(collection.contains(B("5"))) // true
        println(collection.size) // 1
    }
    j
    e
    +2
    • 5
    • 16
  • o

    Olaf Gottschalk

    07/17/2025, 10:47 AM
    I have stumbled upon an internal annotation that I think I would also like to use... In my situation, I want to declare a fun
    holds
    for a DSL that is a generic function very similar to
    in
    - it checks the contents of something against another value. My current implementation looks like this:
    Copy code
    @TransformationDsl
        infix fun <V> ValueProvider<S, V>.holds(value: V): Boolean = value in this
    The problem is, written like this, the user loses type safety as the generic
    V
    will always look for the first suitable "upper" type, being
    Any
    in the worst case. So, usage like this is not prohibited by the compiler:
    Copy code
    val x: Value<String> = TODO()
    if (x holds 42) {
      // ...
    }
    In this case, I want the compiler to note that the Integer 42 can never be contained in the
    Value<String>
    ! Looking at the declaration of the
    operator fun in
    for an Iterable reveals this hack:
    Copy code
    public operator fun <@kotlin.internal.OnlyInputTypes T> Iterable<T>.contains(element: T): Boolean {
        if (this is Collection)
            return contains(element)
        return indexOf(element) >= 0
    }
    If you try to check whether an Iterable of String contains an Int value, the compiler does not allow this. I want exactly the same behaviour, but I cannot use the annotation, because it is internal... Any other way to not allow the generic V to be allowed to be Any? Thanks!
    d
    c
    • 3
    • 4
  • c

    CLOVIS

    08/14/2025, 8:10 AM
    Copy code
    interface A
    class B(val b: Int) : A
    class C(val c: Int) : A
    
    val list: List<A> = // …
    
    val (b, c) = list.partition { it is B }
    println(b.b)    ⚠ doesn't compile! 'b' is of type A
    It would be great if the correct contracts were added to
    .partition
    for this example to work
    ➕ 1
    d
    e
    • 3
    • 2
  • f

    Filipp Zhinkin

    08/15/2025, 5:38 PM
    Hey hey! In
    2.1.20
    Instant
    and
    Clock
    from
    kotlinx-datetime
    were added (and slightly modified along the way) to the standard library as kotlin.time.Instant and kotlin.time.Clock and remained experimental since then. There were almost no feedback on these types (except how painful was the removal of corresponding classes from the datetime library), so they are candidates for stabilization in
    2.3
    . Please let us know if there are any issues with
    kotlin.time.Instant
    and
    kotlin.time.Clock
    API or implementation, so it could be updated before stabilization, if needed. Thanks!
    🐿️ 1
    K 4
    r
    k
    c
    • 4
    • 10