Slackbot
04/19/2021, 12:20 PMgeepawhill
04/19/2021, 1:33 PMmethod(input:Set<X>):Set<X>
, which wants to transform each element of the (small) set, seemingly a natural fit with .map { }
, but, the transformation can produce invalid individual values, and if that occurs the method's result will be emptySet()
. In other words, under some conditions i want to short-circuit the mapping and just return the empty set. I am wondering if there is some clever stream-ish/sequence-ish way to accomplish this, or should I just go with a for-in and a result accumulator?Eivind
04/19/2021, 2:16 PMHarry B
04/19/2021, 8:06 PMVivek Modi
04/19/2021, 9:09 PMnkiesel
04/19/2021, 10:28 PMmapOf("a" to 1L, "b" to 2)
now has a type Map<String, Long>
while it had type Map<String, Any>
in 1.3. This caused a runtime error because the 2
was passed to some Java code which then casts it to an int
. It was easy enough to fix by using `mapOf<String, Any>("a" to 1L, "b" to 2)`but it makes me wonder: could I somehow prevent the automatic promotion of the 2
to a 2L
?Michael Böiers
04/20/2021, 2:50 PMinline fun <T> T.thatIf(condition: Boolean, block: T.() -> T) = if (!condition) this else block()
inline fun <T, R> T.thatIf(condition: Boolean, default: R, block: T.() -> R) = if (!condition) default else block()
inline fun <T> T.letIf(condition: Boolean, block: (T) -> T) = if (!condition) this else block(this)
inline fun <T, R> T.letIf(condition: Boolean, default: R, block: (T) -> R) = if (!condition) default else block(this)
val ALL_TO_LOWER = true
val APPEND_SIZE = false
fun main() {
val foo = "Foo"
.thatIf(ALL_TO_LOWER) { toLowerCase() }
.thatIf(APPEND_SIZE) { "$this ($length)"}
val bar = "Bar"
.letIf(ALL_TO_LOWER) { it.toLowerCase() }
.letIf(APPEND_SIZE) { "$it (${it.length})"}
println("$foo $bar")
}
user
04/20/2021, 2:58 PMAditya
04/20/2021, 4:50 PMpoohbar
04/20/2021, 8:20 PMeygraber
04/21/2021, 12:31 AMAnother change is a set of new factory functions for creatingI saw ☝️ in the release notes for 1.5 RC, and I'm curious what the reasoning is for deprecating the extension properties. I found it to be a very nice API. Was it about polluting the namespace?instances from integer values. They are defined directly in theDuration
type and replace the old extension properties of numeric types such asDuration
.Int.seconds
Grigorii Yurkov
04/21/2021, 7:38 AMif (list is List<MyClass>)
? Compiler forces me to write if (list is List<*>)
. So I have to write if (list as? List<MyClass> != null)
to smartcast my listMarko Novakovic
04/21/2021, 8:46 AMReadWriteProperty
for example, it uses kotlin.reflect.KProperty
. do properties use reflection or KProperty
is there to enable reflection?Jukka Siivonen
04/21/2021, 11:08 AMif (myNullable?.status in listOf(FOO, BAR)) { <myNullable not nullable here> }
edrd
04/21/2021, 1:05 PMdave08
04/21/2021, 1:20 PMinterface UseCase<in Request : Any, out Response: Any> {
suspend fun execute(request: Request): Response
suspend operator fun invoke(request: Request): Response = execute(request)
}
But when I call an implementation of it in a unit test, it throws:
usecase.SomeUseCase.invoke(Ljava/lang/Object;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
java.lang.AbstractMethodError: usecase.SomeUseCase.invoke(Ljava/lang/Object;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
what could this be?Rob Elliot
04/21/2021, 1:42 PMwhen (enumValue) {
Enum1, Enum2 -> doSomethingImperative()
else -> Unit
}
as an alternative to
val certainEnumConstants = setOf(Enum1, Enum2) // defined in an object, so allocated once for the life of the runtime - recent edit in light of confusion in thread
if (enumValue in certainEnumConstants) {
doSomethingImperative()
}
I like the lack of need to define and name a val enums: Set<EnumType>
in the first, but I don’t like the need for an else
branch.Slackbot
04/21/2021, 8:28 PMSlackbot
04/21/2021, 8:41 PMKarlo Lozovina
04/21/2021, 9:58 PMcopy()
function of data classes Intellij highlights the code as a warning (with error of "Unused result of data class copy").
is there a way to get warning activated for my own functions, when I don't use the result value?Shailan Patel
04/22/2021, 12:22 AMSimon Lin
04/22/2021, 5:53 AMval a: Int? = null
a.foo { 0 } // 0
val b: Int? = 1
b.foo { 0 } // 1
Slackbot
04/22/2021, 7:00 AMuser
04/22/2021, 10:08 AMNikolay Puliaev
04/22/2021, 11:34 AMval red = data.red.map { it.toUByte().toInt() }.average()
val green = data.green.map { it.toUByte().toInt() }.average()
val blue = data.blue.map { it.toUByte().toInt() }.average()
val luminance = red + green + blue
Cicero
04/22/2021, 12:02 PMHovhannes
04/22/2021, 1:20 PMDavid Smith
04/22/2021, 3:48 PMIterable<T>.map
returns a List<R>
not the original data structure (whatever implemented the Iterable
. This seems very strange to me, is it expected? I would expect the map
function to affect only the contents of the data structure, not the data structure itself.Christian Dräger
04/22/2021, 5:44 PMfoo { // will invoke some object as this
0 {
// create some list based on Foo and get 0 index of list
}
* {
// create some list based on Foo and get all entries of list
}
}
https://kotlinlang.org/docs/operator-overloading.htmlpascalchidi
04/22/2021, 10:58 PMpascalchidi
04/22/2021, 10:58 PMAlexey Belkov [JB]
04/23/2021, 8:43 AM