Chilli
11/27/2020, 11:54 PMLazard
11/28/2020, 1:07 PMCicero
11/28/2020, 2:25 PMval incomingString = "A lot of things that are not interesting but this: ThePath"
when(incomingString){
"The Path" -> something
"No Path" -> something
else -> somethingElse
}
Better exampleChilli
11/28/2020, 6:35 PMDieter Konrad
11/28/2020, 7:35 PMKotlinx.html
considered type safe?Zimri Leisher
11/28/2020, 9:09 PMTwoClocks
11/28/2020, 10:34 PM[-1,-1,-1,1,1,-1,-1]
should yield [-3,2,-2]
I can't figure out how to do this w/o iterating over the list, and keeping a bunch of mutable state. Is there fancy sequence of collection functions I could use to do this?Byron Katz
11/28/2020, 10:37 PMFlorian
11/29/2020, 8:04 AMList<User>
and want to get a List<String>
of their name
property, is there a helper method for this?Animesh Sahu
11/29/2020, 9:43 AMvar (e, f, c, d) = arrayOf(1, 10, 12, 55)
Seems to decompile to:
Integer[] var6 = new Integer[]{5, 44, 34};
boolean var8 = false;
int var2 = ((Number)var6[0]).intValue();
var8 = false;
int var3 = ((Number)var6[1]).intValue();
var8 = false;
int var4 = ((Number)var6[2]).intValue();
var8 = false;
int d = ((Number)var6[3]).intValue();
Is there a workaround to declare multiple variables concisely but also with an efficient handling performance wise?SrSouza
11/29/2020, 4:46 PMhttps://www.youtube.com/watch?v=iYhOU9AuaFs▾
Animesh Sahu
11/30/2020, 3:22 AMByron Katz
11/30/2020, 5:45 AMsns
11/30/2020, 7:48 AMAnimesh Sahu
11/30/2020, 7:52 AMRob Elliot
11/30/2020, 8:31 AMRobert Jaros
11/30/2020, 1:35 PMAnimesh Sahu
11/30/2020, 3:46 PMuser
11/30/2020, 5:32 PMAnimesh Sahu
12/01/2020, 4:28 AMPavel Sidyakin
12/01/2020, 1:35 PMimport androidx.annotation.FloatRange
typealias ProgressFloatRange = FloatRange(from = 0.0, to = 1.0, fromInclusive = true, toInclusive = true)
Big Chungus
12/01/2020, 2:20 PMjvmusin
12/01/2020, 4:05 PMkotlin.collections.sumOf
.
I find it very useful, but I have a use case when I want to overload this function.
Suppose I have a simple class IntHolder
and an extension function Iterable<T>.sumOf((T) -> IntHolder)
.
How can I use both mine sumOf
and prewritten sumOf
from kotlin.collections
?
I have some workarounds like renaming this method or using import as
for a kotlin.collections.sumOf
, but those are not solutions, but just workarounds.
Maybe you know the reason it doesn't work or you know how to make it work?
As for me, right now it looks like a bug.
I've already asked about this on StackOverflow but got no responses Here.
The real use case is actually not that simple, but I believe the code below totally describes the whole situation.
import kotlin.experimental.ExperimentalTypeInference
data class IntHolder(val x: Int) {
operator fun plus(other: IntHolder) = IntHolder(x + other.x)
}
@OptIn(ExperimentalTypeInference::class)
@OverloadResolutionByLambdaReturnType
inline fun <T> Iterable<T>.sumOf(f: (T) -> IntHolder) = fold(IntHolder(0)) { acc, i -> acc + f(i) }
fun main() {
val range = 1..5
val a = range.sumOf { it * 2 } //here is an error
val b = range.sumOf { IntHolder(it * 2) }
}
Nir
12/01/2020, 8:55 PMmyMap[k] = myMap[k]!! + 1
or similar, to update the map. Kotlin doesn't seem to allow things like myMap[k]!! += 1
either. Wouldn't it be useful to have a modify
or update
function? Is there one already that I missed? It clarifies intent and avoids the repetition of the key:
fun MutableMap<K, V>.update(k: K, transform: (V) -> V) { this[k] = transform(getValue(k)) }
fun MutableMap<K, V>.update(k: K, default: V, transform: (V) -> V) { this[k] = transform(getOrDefault(k, default)) }
Usage would mean that instead of myMap[k] = myMap[k]!! + 1
you'd be able to write myMap.update(k) { it + 1}
Antoine Gagnon
12/01/2020, 9:35 PMopen class AnimalBuilder {
val dog: DogBuilder get() = DogBuilder()
open inner class DogBuilder : AnimalBuilder() {
val pug: PugBuilder get() = PugBuilder()
inner class PugBuilder : DogBuilder()
}
}
val pugBuilder = AnimalBuilder().dog.pug
val wrongBuilder = AnimalBuilder().dog.dog.dog
But as you can see, I don’t want the DogBuilder
to be able to access the dog
variable available in AnimalBuilder
. Is there any way to do that?pniederw
12/01/2020, 11:10 PMCicero
12/01/2020, 11:30 PMprivate var filteredList = mutableListOf<Any>()
if(filteredList.contains(item.resourceType)
data class Item (
val resourceType: ResourceType? = null
)
Now if I give it the ResourceType straight it works. But I would like to understand why not.
PS: The way I’m going around this, for now, I’m surrounding the case with when(type) and giving the type class directly into it ex:
if(filteredList.contains(ResourceType)
bjonnh
12/02/2020, 1:47 AMandylamax
12/02/2020, 6:51 AMAnimesh Sahu
12/02/2020, 7:08 AMAnimesh Sahu
12/02/2020, 7:08 AMArkadii Ivanov
12/02/2020, 9:57 AM