Phani Mahesh
10/02/2020, 2:34 PMcsvReader.open(file) { ... }
function from a library. The problem is the lambda is not suspend-aware. Is there any way to call a suspend fn in the lambda there other than calling runBlocking? FWIW, this is all inside a suspending function, so I do not want to do runBlocking
inside a suspend
function. It just feels so wrong.LeoColman
10/02/2020, 5:08 PMbbaldino
10/02/2020, 8:53 PMclass Foo {
fun String.someStringExtension() { ... }
}
fun Foo.String.someOtherStringExtension() { ... }
Joel
10/02/2020, 8:57 PMAnimesh Sahu
10/03/2020, 3:39 PMfun <T : SomeClass<*>> SomeOtherClass<*>.function() {...}
Such that we can guarantee the two star-projections must be equal? I know we can introduce another generic, but that'll lead to redefine that explicitly twice like function<T<E>, E>
when calling the function. So is there any way we can omit that extra generic and use some implicit kind of thing?CLOVIS
10/04/2020, 2:10 PMx
is kotlinx
for ‘extensions' (like in javax
) or for ‘experimental'?addamsson
10/04/2020, 2:49 PMfun someFun(fn: (SomeClass) -> Unit)
I have to explicitly return Unit.INSTANCE
from Java code. Is there a way to fix this (besides writing a helper function)Uzi Landsmann
10/04/2020, 7:22 PMDuration
implementation in kotlin.time
I can see the extension methods from Int
and Long
to Duration
. However I’d like to use the Java function:
public static Duration ofSeconds(long seconds, long nanoAdjustment)
…which I can’t see an implementation for. Any thoughts on how to achieve the same in kotlin?Andrea Giuliano
10/05/2020, 7:55 AMval bos = ByteArrayOutputStream()
DataOutputStream(bos).use {
it.writeInt(mydata)
}
ByteBuffer.wrap(bos.toByteArray())
I was googling around but haven’t found anything really.zeugederunity
10/05/2020, 8:46 AMfun main() {
fun main() {
val arr1 = Array(0) { "" }::class
val arr2 = arrayOf("")::class
val arr3 = Array::class
println(arr1 == Array::class) //false
println(arr1.isSubclassOf(Array::class)) //false
println(arr2 == Array::class) //false
println(arr2.isSubclassOf(Array::class)) //false
println(arr3 == Array::class) //true
println(arr3.isSubclassOf(Array::class)) //true
}
I think thats a bug.Xitij
10/05/2020, 8:49 AMsuspend fun jobLifecycle () = coroutineScope {
val job = launch{
println("Working....")
delay(2000L)
println("Done !")
}
delay(1000L)
job.cancel()
// delay(1L) i have to do it to print correct value of job.isCompleted below
println("isCompleted: ${job.isCompleted}") // it is printing "false" but if i delay for some time after cancelling the job it prints "true".
}
is it my computer or it really takes some time to move job from cancelling to cancelled state where isCompleted becomes true after cancellation of job or is there something im missing out pls review..Tobias G. Waaler
10/05/2020, 10:30 AMAny?
when the method I'm overloading has return type Object
🤔Philipp Mayer
10/05/2020, 11:49 AM.map { (payment, requestModel) -> steps.performRequest(payment, requestModel)
.filter { (_, result) -> result is ApiResult.Success }
.map { (payment, result) -> steps.update(payment, result) }
After performing the request, I want to filter out all results which were not of Type ApiResult.Success
Unfortunately the compiler does not allow this, since update()
is expecting ApiResult.Success
.
What did I miss there?
My only solution right now is to set the result
Parameter to ApiResult and add a require()
block in my update function, but I'm not really happy with that.Vampire
10/05/2020, 4:28 PMUnnecessary safe call on a non-null receiver of type Int?Why is
Int?
non-null?
Why is it wrong to use ?.
on it?user
10/05/2020, 5:43 PMSubhi Pandey
10/05/2020, 6:10 PMmike.holler
10/05/2020, 8:03 PMsnowe
10/05/2020, 11:09 PMfor (Map<String, String> serviceOutputs; (serviceOutputs = resultReader.read(header)) != null; ) {
try {
final double elapsed = Double.parseDouble(serviceOutputs.get("__elapsed"));
gets converted to
var serviceOutputs: Map<String, String>
while (resultReader.read(*header).also { serviceOutputs = it } != null) {
try {
val elapsed = it["__elapsed"]!!.toDouble()
which doesn’t work because it
can be null, and serviceOutputs
can’t be null.
I can correct this to:
var serviceOutputs: Map<String, String>?
while (resultReader.read(*header).also { serviceOutputs = it } != null) {
serviceOutputs?.let {
try {
val elapsed = it["__elapsed"]!!.toDouble()
but that is pretty messy and not indicative at all of what is happening. serviceOutputs
can never be null in the block, and using a let
is messy. Any way to accomplish this a bit cleaner?Animesh Sahu
10/06/2020, 5:02 AMTimur Atakishiev
10/06/2020, 7:41 AMinline fun <reified T> isTypeOf(any: Any?): Boolean {
return any is T
}
val isTypeOf: Boolean = isTypeOf<Collection<String>>(Any)
my Any is ArrayList of String.Andrew Gazelka
10/06/2020, 8:47 AMjanvladimirmostert
10/06/2020, 10:01 AM.editorconfig
but it doesn't seem to do anything
ij_any_if_brace_force = always
ij_any_for_brace_force = always
Ktlint seems to have such options, but it's marked as experimental and it will only show warnings
What are other teams using for automated code formatting in Kotlin?Jordi Pradel
10/06/2020, 1:20 PMasync()
exposes users to exceptions:
Coroutine builders come in two flavors: propagating exceptions automatically (launch and actor) or exposing them to users (async and produce).But then, I'd expect wrarping the
await()
call in try... catch
would just allow me to catch the exception produced in the async
block. Instead, it seems the async()
block's exception is handled by the supervisor. That gives me an odd behavior in this example:
import kotlinx.coroutines.*
fun main() = runBlocking<Unit> {
try {
val res = async {
// suspend fun that ends up throwing ArithmeticException
failedConcurrentSum()
}
res.await()
} catch(e: ArithmeticException) {
println("Computation failed with ArithmeticException")
}
}
The odd part is that the exception is caught by catch
but, at the same time, it is thrown by runBloking.
See an executable snippet here: https://pl.kotl.in/sWjfVuEDr.
Why is that so? What am I missing here??kevinherron
10/06/2020, 2:18 PMNir
10/06/2020, 3:59 PMinline fun <T, K, R> Grouping<T, K>.fold(
initialValueSelector: (key: K, element: T) -> R,
operation: (key: K, accumulator: R, element: T) -> R
): Map<K, R>
Semi related but the relationship between aggregate
and fold
for Grouping
seems kind of fuzzy to me. It seems like this version of fold
is just an awkward, two lambda version of aggregate. But then, the simplest case is handled most nicely by the fold
overload taking a simple initial value. So the useful functionality is kind of spread between them, and yet aggregate is missing the most useful overload (IMHO).
Just curious if there are reasons I'm missing why the API is this way. The first time I went to use Grouping
it felt a bit clunkier than I'm used to in Kotlin.Philipp Mayer
10/06/2020, 7:24 PMreturn when ((1..2).random()) {
1 -> "ABC"
2 -> "XYZ"
else -> "???"
}
The range only includes 1 and 2 and random returns an int.. 🤔Karlo Lozovina
10/06/2020, 7:28 PMPhilipp Mayer
10/07/2020, 6:29 AMAnimesh Sahu
10/07/2020, 1:47 PMAlex
10/08/2020, 12:07 PMAlex
10/08/2020, 12:07 PMVampire
10/08/2020, 12:07 PMAlex
10/08/2020, 12:08 PMVampire
10/08/2020, 12:10 PMAlex
10/08/2020, 12:10 PMkqr
10/08/2020, 4:10 PM