rrva
11/24/2021, 7:37 AMdistinctBy { <http://it.id|it.id> }
be more efficient over distinct() for those object having an id field?Big Chungus
06/06/2022, 8:56 PMkotlin-stdlib
is published without the gradle metadata? https://repo1.maven.org/maven2/org/jetbrains/kotlin/kotlin-stdlib/1.7.0-RC2/Niels van Velzen
04/03/2023, 12:54 PMMarcin Wisniowski
04/04/2023, 9:47 PMlist.groupBy { it.property }.map { (_, list) -> list.first() }
Is there a simpler way? This is something I would normally solve with .toSet()
, except I only want to consider one property of the data class, not the entire item.Ryan Smith
04/06/2023, 6:33 PMPreconditions.kt
as part of the implementation of check{,NotNull}
and require{,NotNull}
et al.
Today I discovered I can write similar blocks in my own code like this as long as I opt-in for `@ExperimentalContracts`:
fun checkSomeType(value: SomeSuperType) {
contract {
returns() implies (value is SomeType)
}
check(value is SomeType) {
"error"
}
}
My question is multi-faceted. Has the contracts API always been "experimental"? If so, what's left for it to become "stable" given how long it's been around? If not, is it marked "experimental" because it's becoming part of the public standard library API?CLOVIS
04/07/2023, 2:57 PMComparable
from a Comparator
? If there is none, I think it would be a nice addition, to make this possible:
class Foo(…) : Comparable<Foo> by fooComparator.asComparable() {
companion object {
private val fooComparator = compareBy({…}, {…}) // use the stdlib convenience functions for comparators
}
}
Otherwise, it's not possible to reuse these convenience functionsPHondogo
04/11/2023, 1:15 PMRyan Smith
04/13/2023, 11:32 PMNorbi
04/16/2023, 1:05 PMNumber
is expected (in my case it was a generic function with a type parameter <T: Number>
).
I have checked the corresponding KEEP and found this:
is an abstract class in Kotlin, and inline classes are not allowed to extend other classes, therefore unsigned types can't be inherited fromNumber
class.Number
We haven't found compelling use cases to circumvent this limitation specially for the unsigned types.My questions: 1. Is it really "good enough" to have non-unified "number" handling in Kotlin? 2. Why is
Number
an abstract class
and not an interface
? It has only some abstract functions, nothing else.
Thanks.Youssef Shoaib [MOD]
04/21/2023, 1:50 PMreturns
with callsInPlace
. For instance, I have an inline higher-order function that takes an enabled: Boolean
parameter, and it only calls the block passed to it if enabled
is true. How do I say that enabled
is true
inside of the block
?
E.g.:
@OptIn(ExperimentalContracts::class)
inline fun runIf(condition: Boolean, block: () -> Unit) {
contract {
callsInPlace(block, InvocationKind.AT_MOST_ONCE)
}
if(condition) block()
}
fun main() {
val test: String? = "Hello"
runIf(test != null) {
test.length
}
}
Yes that example is stupid, but my use-case provides parameters to block
and hence it can't just return a Boolean
nkiesel
04/21/2023, 3:43 PMRob Elliot
04/24/2023, 2:10 PMSet<Char>
implementation than setOf(' ', '\n', ...)
for checking if an input character is in a particular set of characters?Derek Peirce
05/03/2023, 12:34 AMequals
, hashCode
, and toString
for collections that each take a lambda, so you can specify a specific field that you want to check for equality or a substitute method to determine if two elements are equal, and so on. I don't think there's a simple way to write the equals
currently without involving sequences (preventing inlining), ensuring same size and then all
on indices and manually comparing (which is how I expect a method to be implemented, edit: that requires efficient random access, using the iterators directly would be required), or mapping each list to the desired field to compare or hash or whatnot (which means creating two unnecessary intermediate collections).jw
05/11/2023, 1:44 AMAutoCloseable
set to be stable LeoColman
05/11/2023, 2:30 PMJavier
05/25/2023, 9:39 AMtypealias File = java.io.File
to the kotlin stdlib in the jvm part would be great, so we don't have to remember to add the import.
But I don't know if there are plans to add a real multiplatform File
to the stdlib "soon"Nir
05/29/2023, 4:34 PMfun main() {
val x = Double.NaN
println(x == x)
println(listOf(x) == listOf(x))
}
Nir
05/29/2023, 4:35 PMfalse
true
dimsuz
05/31/2023, 1:30 PMobject Stuff
and call toString()
on it, it includes hex. I recall there were some news about nicer toString for objects, but I can't find them now, maybe someone remembers?andries.fc
06/05/2023, 2:48 PMDerek Peirce
06/14/2023, 1:01 AMjoinToString
, but instead of a transform
lambda, it takes build: StringBuilder.(T) -> Unit
. That avoids creating intermediate strings for each element if multiple append
calls would be more efficient.Youssef Shoaib [MOD]
06/14/2023, 2:44 AMjoinToString
not being inline has been a (rather mild) inconvenience for me. Is there a reason why it isn't? I know that the stdlib is using a default value of null
for the transform lambda, but couldn't it just use it.toString()
?Zoltan Demant
06/14/2023, 1:37 PMMichael de Kaste
06/16/2023, 7:39 AMorNull
functions in mind, I was unpleasantly surprised there is no such thing as List<T>.indexOfFirstOrNull
kevin.cianfarini
06/27/2023, 2:22 PMOpenEndRange
might be stabilizedelect
06/30/2023, 8:19 AMIntArray
with a given comparator? I see only sortedWith
, which returns a new sorted listspleenjack
06/30/2023, 8:57 AMmapOfNotNull
in stdlib to build a map from a list of nullable pairs from which only non-null pairs will be taken? There are two other functions (listOfNotNull
and setOfNotNull
) here and it seems quite reasonable to have a similar function for maps for the sake of consistency. I know that you can just write your own implementation, but it’s unlikely that you will do it most optimal and correct way (just for example, you should probably not forget about inlined mapOfNotNull() = emptyMap()
, and etc).Zoltan Demant
07/10/2023, 12:22 PMWukongRework.exe
07/24/2023, 8:48 PMrunningReduceRight
and `runningFoldRight`/`scanRight` for symmetry with the reduce
and reduceRight
/ fold
and foldRight
functions. Is there a technical reason behind this? I often find myself ad-hoc-ing foldRight
to be a scanRight
for LeetCode and other competitive programming problems.Zoltan Demant
07/26/2023, 11:21 AM@JvmInline
value class Token(
private val value: String,
) {
constructor() : this("1337") {
// **THIS**
println(value)
}
}