diego-gomez-olvera
02/18/2025, 11:53 AM@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 laterPHondogo
02/19/2025, 6:20 PMRohde Fischer
02/21/2025, 10:05 AMinterface Foo {
val selfValue: String
fun doSomething(): String {
if (this::class == Foo::class) {
return "Foo"
} else {
return "${super.doSomething()}.$selfValue"
}
}
}
Klitos Kyriacou
02/21/2025, 1:56 PMval 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:
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?George
03/04/2025, 4:30 PMCLOVIS
03/22/2025, 10:11 AMTreeMap
in the standard library?Javier
03/23/2025, 1:27 PMkotlin
and not the one from Kotlinx
is still necessary.Klitos Kyriacou
04/02/2025, 12:04 PMlistOf(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?ghedeon
04/19/2025, 5:22 PMbisect_left
, bisect_right
)? Sad java Collections legacyCLOVIS
04/19/2025, 5:45 PMIntRange.size
… Am I missing something?Eugen Martynov
04/20/2025, 12:26 PMVasily
04/21/2025, 12:14 PMfun <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?CLOVIS
04/25/2025, 9:58 PMand
or or
operators are in package kotlin.experimental
? 🤔
https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.experimental/and.htmllouiscad
04/28/2025, 12:18 PMAtomicLong
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.Klitos Kyriacou
04/28/2025, 5:21 PMveryLargeList.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?
veryLargeList.asSequence().sorted().take(10).toList()
louiscad
05/21/2025, 12:15 PMSystem.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…Zoltan Demant
05/30/2025, 2:15 PMpackage 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,
)
}
CLOVIS
06/04/2025, 8:32 PMUuid
and Instant
@Serializable
?Eugen Martynov
06/06/2025, 3:40 PMUuid.parseOrNull()
Slackbot
06/06/2025, 4:25 PMZoltan Demant
06/09/2025, 3:55 AMtimestampOffset
always being 0 here, but its not the case. Compiler bug?
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"
Zac Sweers
06/09/2025, 1:03 PMsortedSetOf()
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-20972Derek Peirce
06/13/2025, 5:46 AMRegex("""java\.lang\.ArrayIndexOutOfBoundsException: Index \d+ out of bounds for length \d+""")
I could write:
Regex("java.lang.ArrayIndexOutOfBoundsException: ", RegexOption.LITERAL) + Regex("""Index \d+ out of bounds for length \d+""")
Raid Xu
06/13/2025, 7:07 AMReentrantReadWriteLock.write
in Locks.kt
is marked as InlineOnly
. Is it to forbid Java from calling it? But what's reason behind it?Marcin Wisniowski
06/25/2025, 1:40 PMlistOf(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.Eugen Martynov
07/02/2025, 12:11 PMLong.toHexString()
produces a different string comparing to Java one? There are extra 0
paddingZoltan Demant
07/03/2025, 11:27 AMsealed 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
}
Olaf Gottschalk
07/17/2025, 10:47 AMholds
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:
@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:
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:
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!CLOVIS
08/14/2025, 8:10 AMinterface 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 workFilipp Zhinkin
08/15/2025, 5:38 PM2.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!