Olaf Gottschalk
02/10/2025, 4:02 PMNumber.toBigDecimal()
? I struggle with this a lot as there are specific functions Int.toBigDecimal()
and so on, even for the real tricky cases like Double.toBigDecimal()
which does the "right" thing using the String constructor of BigDecimal
rather than the double constructor which may lead to real spooky results...
So in my code, I write my own extension function:
fun Number.toBigDecimal(): BigDecimal = when(this) {
is Short -> this.toBigDecimal()
is Int -> this.toBigDecimal()
.......
}
This is highly repetitive and error prone... so, why is this function just missing from the standard lib?Youssef Shoaib [MOD]
02/12/2025, 7:34 PMResult
the way that it is? There is a potentially more efficient encoding where you leave failures unboxed, leave most successes unboxed, and only box successes that happen to be throwable. In other words, having the internal encoding be a union of Throwable | (R - Throwable) | SuccessfulThrowable<R>
Thomas
02/13/2025, 6:17 AMKInterface
exist in kotlin.reflect
?
Its interesting that KClass
has a boolean property isFun
, that shows whether the class is a functional interface... Could this be related?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
}