Dominaezzz
11/15/2021, 1:09 PMholgerbrandl
11/16/2021, 9:17 AMMichael
11/16/2021, 8:42 PMAnsh Tyagi
11/17/2021, 9:24 PMMultiMap
in Kotlin? I wanted to reproduce the following guava data structure into native data structure
MultimapBuilder.treeKeys().arrayListValues().build<Int, Move>()
okarm
11/18/2021, 12:00 AMList.fastForEach
https://cs.android.com/androidx/platform/frameworks/support/+/androidx-main:compose/runtime/runtime/src/commonMain/kotlin/androidx/compose/runtime/snapshots/ListUtils.kt;l=27
/**
* Iterates through a [List] using the index and calls [action] for each item.
* This does not allocate an iterator like [Iterable.forEach].
*/
@OptIn(ExperimentalContracts::class)
internal inline fun <T> List<T>.fastForEach(action: (T) -> Unit) {
contract { callsInPlace(action) }
for (index in indices) {
val item = get(index)
action(item)
}
}
Is this something that the standard library might be interested in? Yes/no and why?holgerbrandl
11/18/2021, 7:31 PMDanish Ansari
11/21/2021, 8:03 AMkotlin.math.max()
and kotlin.comparisons.maxOf()
? Both of them seem to be doing the same thing.
2 things I noticed that the later one can compare 3 values also and we can also pass our own comparator. Any other difference than these?spand
11/23/2021, 9:11 AMflatten()
with a transform
been considered ? Seems like an obvious addition which would enable us to skip a list allocation (we have a bunch of flatten().map { it.idOfEntity }
) not to mention it being a bit simpler: flatten { it.idOfEntity }
A difference with normal flatten
would be that it no longer calls addAll()
which allows it to possibly skip a few grows but given that it can skip an entire copy on the next map
in our case it seems like it should win out. Maybe that would be a reason for keeping both as overloads and not add transform with default parameter.
Example implementation:
public inline fun <T, R> Iterable<Iterable<T>>.flatten(transform: (T) -> R): List<R> {
val result = ArrayList<R>()
for (elementIterable in this) {
for (element in elementIterable) {
result.add(transform(element))
}
}
return result
}
rrva
11/24/2021, 7:33 AMMap<String, T> + Map<String, T>
as well as Map<String, T>.distinct()
and this is showing up during profiling as a hotspot, is there some obvious optimization I can do that lets me keep calls to plus
and distinct
but makes it go faster? All involved classes are kotlin data classes so implement that hashCodeDanish Ansari
12/02/2021, 2:52 PMdimsuz
12/09/2021, 12:00 PMfun <R> task(body: () -> R): R = TODO()
fun <P1, R> task(body: (P1) -> R): R = TODO()
fun main() {
// overload ambiguity
task {
33
}
}
But no ambiguity for these two:
fun <P1, R> task(body: (P1) -> R): R = TODO()
fun <P1, P2, R> task(body: (P1, P2) -> R): R = TODO()
fun main() {
task { v: Int -> 33 }
}
Greg Rynkowski
12/13/2021, 5:17 PMOrhan Tozan
12/17/2021, 3:34 PMjava.lang.IllegalStateException: Symbol for kotlin.collections/mutableMapOf|-4813910536206556932[0] is unbound
at org.jetbrains.kotlin.ir.symbols.impl.IrBindablePublicSymbolBase.getOwner(IrPublicSymbolBase.kt:52)
Exerosis
12/18/2021, 9:55 PMMustafa Ozhan
12/21/2021, 5:28 PMResult
class with Kotlin Result then I just realised that my coverage was decreased .
Unfortunately none of the onSuccess
and onFailure
methods were covered.
• Is there any plan of covering them ?
• How do you guys cover these methods are you writing Unit test on Kotlin Result methods ?SecretX
12/22/2021, 8:41 PMConcurrentHashMap<Int, String>().getOrPut(1) { null } // compiles just fine, but it'll throw in runtime
mutableMapOf<Int, String>().getOrPut(1) { null } // compiler error: Null can not be a value of a non-null type String
Astronaut4449
12/27/2021, 4:39 PMto
from the stdlib and make it some encapsulated function of a map builder scope so that we can write:
val squared = mapOf { // this refers to some MapBuilderScope<K,V> with builder inference
1 to 1 // `to` is only visible in the `MapBuilderScope`
2 to 4
3 to 9
}
smallufo
12/28/2021, 5:57 PMfun getMap(): Map<String, String> {
return buildMap {
if (A) {
put(key, "A")
} else {
put(key, "B")
}
}
}
but after upgrade to kotlin 1.6 , compiler complains type mismatch :
Type mismatch.
Required:
Unit
Found:
String
Type mismatch.
Required:
Unit
Found:
TypeVariable(V)?
Type mismatch.
Required:
Unit
Found:
String?
I have to change to this style :
return buildMap {
put(key, if (A) "A" else "B")
}
It is OK but not so readable.
But why !?Ruckus
12/30/2021, 5:40 AMtypealias Null = Nothing?
to the stdlib (and preamble)? Common use case:
class Info<T>(val description: String, val payload: T, ...)
interface Base<T> {
fun getInfo(): List<Info<T>>
}
class Derived: Base<Null> {
override fun getInfo() = listOf(Info("...", null, ...), ...)
}
IMO it reads easier than class Derived: Base<Nothing?>
.
(Yes, I understand I could use Unit
in this case to achieve basically the same thing in this contrived example. That's beside the point.)holgerbrandl
01/02/2022, 1:36 PMkotlin.time.Duration
to a java.time.temporal.TemporalAmont
? E.g. to do Instant.now() + 5.seconds
. With the JDK Duration its easy, because it implements TemporalAmont
.xxfast
01/03/2022, 11:05 PMwe can <u>underline</u>,<b>bold</b> or <i>hyphenate</i>
in commonMain
?Ayfri
01/05/2022, 1:11 PMfun String.remove(pattern: String) = replace(Regex(pattern), "")
fun String.remove(regex: Regex) = replace(regex, "")
Or at least the latter one ?
I also have in multiple projects this extension
fun String.get(regex: Regex) = replace(regex, "$1")
That could be useful to addholgerbrandl
01/09/2022, 7:40 PMNumber.minOrNull()
?Ayfri
01/11/2022, 6:43 PM.max()
and .min()
methods have been removed ?spand
01/12/2022, 4:28 PMfun <K, V> Iterable<Map<K, V>>.merge(): Map<K, V>
that I am missing ?Fleshgrinder
01/14/2022, 3:02 PM-jdk7
and -jdk8
to the dependencies (some build tools can do this automatically). Considering that there are a few nice features like indy
that could be enabled also for the stdlib if it were to be compiled for newer versions. Or could this create issues for transitive dependencies that were compiled against a different Kotlin dependency where they expect it to be Java 1.6 ABI?Chris Fillmore
01/20/2022, 3:35 AMkotlin.Result
if I’m already using a custom Result type? This is my reasoning:
Pros of adopting `kotlin.Result`:
• De-duplication
• Don’t need to maintain my own type
• A portion of my code is essentially an SDK and it would be nice for its clients not to have to use my custom type, and could instead depend on Kotlin’s
• Better performance than my current type, since mine is not a value
class (though it’s probably negligible in my use case)
Cons:
• My custom type is tailored somewhat, providing additional error detail data for e.g. analytics, so I would need to create a custom Exception to carry this information
• My custom Failure type doesn’t require an exception, and so can be used for other logical failures without an exception
• More development on kotlin.Result
seems to be a no-go? (Per the KEEP https://github.com/Kotlin/KEEP/blob/master/proposals/stdlib/result.md#integration-into-the-language)
Anything I may not be considering? Also, any more detail on this:
We are working in other directions of making signalling error handling more pleasant to use in Kotlin.
Greg Rynkowski
01/26/2022, 10:07 AM// all T - java classes implementing ViewDataBinding, each providing a static `bind` method
// ViewDataBinding - a Java abstract class (with no declaration of `bind`)
interface BindingAware<T : ViewDataBinding> {
var f: T
fun bind(v: View) {
// f = T.bind(a) // <= how to call static method provided by class behind T?
}
}
How to call static method of T, having only T?Forrest Pangborn
01/29/2022, 3:54 PMexpect
+ actual
within non-multiplatform modules (or even with a single sourceset) was intentionally supported behavior, but I can't seem to find that anywhere now. does anyone know if this is still the case?
everywhere I can find it on the kotlin site, it only seems to reference/encourage use of this mechanism in cases of expect
in a common module and actual
in platform modules.
i'm looking to use it with a single common sourceset and wondering if this is something i ought to actually avoid, in case it may become unsupported.Rob Elliot
01/30/2022, 3:00 PMfun <T> Iterable<T>.withIndex(): Iterable<IndexedValue<T>>
to include in the stdlib:
fun <T> Iterable<IndexedValue<T>>.withoutIndex(): List<T> = map(IndexedValue<T>::value)