Adam S
04/22/2023, 11:43 AMEugen Martynov
06/27/2023, 2:19 PMSiva
06/28/2023, 4:26 PMadte
07/02/2023, 5:19 PMEugen Martynov
07/11/2023, 2:06 PMSebastian Schuberth
07/18/2023, 1:39 PMStanislav Maksimov
08/09/2023, 1:57 PMShantanu Jaiswal
08/17/2023, 4:16 PMMark Vogel
08/23/2023, 5:30 PMinline fun <T1, T2, R> BsonReader.read(
prop1: KProperty<T1>, read1: BsonReader.(BsonType) -> T1,
prop2: KProperty<T2>, read2: BsonReader.(BsonType) -> T2,
create: (T1, T2) -> R
): R {
readStartDocument()
val name1 = prop1.encodedName
val name2 = prop2.encodedName
var val1: T1
var val2: T2
var readVal1 = false
var readVal2 = false
var type = readBsonType()
while (type != BsonType.END_OF_DOCUMENT) {
when (val name = readName()) {
name1 -> { val1 = read1(type); readVal1 = true }
name2 -> { val2 = read2(type); readVal2 = true }
"_id" -> readObjectId()
else -> error("unknown field name: $name")
}
type = readBsonType()
}
readEndDocument()
require(readVal1) { "no value for $name1" }
require(readVal2) { "no value for $name2" }
return create(val1, val2) // Error: variable must be initialized
}
At the end of the function I know that the variable was initialized, but the compiler can't pick that up.
I can't make the variable nullable otherwise I will have to do a null assertion and the point is to allow the generics to be nullable at the source
Does anyone know of a way to assert that the val1
and val2
are initialized to the compiler?
The only thing I've been able to think of is trying to pass the variables into Java code, but it seems really clunky to try that if that'd even workMark Vogel
08/25/2023, 3:10 PMyield
keyword.
var result = switch (value) {
case "something" -> "";
case "other" -> {
if (true) {
yield "test";
}
// Do something else etc.
yield "";
}
default -> "";
};
Does Kotlin have something analogous? It seems return@when
doesn't work and that's all I can think of, but can't find anything online that talks about itRohan Maity
10/10/2023, 12:37 PMarnaud.giuliani
10/31/2023, 7:03 AMxxfast
11/02/2023, 5:31 AMJakub Gwóźdź
11/08/2023, 8:08 AMCompletableFuture.asDeferred()
already, but when the API you’re using is still java7-compatible, tough luck.
So far I just went with
inline fun <reified T> Future<T>.asCompletableDeferred(): Deferred<T> =
CompletableFuture.supplyAsync { this.get() }.asDeferred()
and it works, as far as I can see.
But I’d like to omit unnecessary wrapping if this
is already Completable:
inline fun <reified T> Future<T>.asCompletableDeferred(): Deferred<T> =
if (this is CompletionStage<*>) this.asDeferred() else CompletableFuture.supplyAsync { this.get() }.asDeferred()
and that is going all hysterical complaining that this.asDeferred()
is no longer Deferred<T>
but Deferred<Any>
Why is that so and how to fix it?Alexander Städing
11/23/2023, 11:01 AMjava.lang.IllegalAccessError
. For some reason, I can't access methods from a companion object:
java.lang.IllegalAccessError: class org.anvilpowered.catalyst.api.config.CatalystKeys$special$$inlined$buildingSimple$1 tried to access field org.anvilpowered.anvil.core.config.Key$Companion.$$INSTANCE
I'm using the latest Kotlin version 1.9.20. I first thought it was due to Java 21 support not being quite there yet, but it also happens with Java 17. Nothing in this class is private or protected.
Here is the class that is being accessed: https://github.com/anvilpowered/anvil/blob/44a541f5ba065b2c05a4ecb00ea4e9930d9f6ad[…]/core/src/main/kotlin/org/anvilpowered/anvil/core/config/Key.kt
Here is the code accessing the class: https://github.com/anvilpowered/catalyst/blob/8d89883a4f30fe80c1045feec0c5c3dc6269[…]ain/kotlin/org/anvilpowered/catalyst/api/config/CatalystKeys.kt
Here is the full stacktrace: https://pastie.io/zccmsw.hs
Is this a Kotlin bug or am I doing something wrong? Let me know if I should create a minimal example. Thanks for any advice.Sebastian Schuberth
11/24/2023, 3:40 PMval id: Int
and in the other interface there is val id: String
. How would I resolve that in my implementation?juliocbcotta
01/02/2024, 11:40 AMsealed interface Container {
interface Foo : Container
interface Bar : Container
}
In some other package, this is what we can get today
sealed class BaseContainer {
data class FooImpl(val something: Something) : Container.Foo
object BarImpl: Container.Bar
}
With this I can't use BaseContainer
instances as Container
in an exhausting when
without a explicit casting as BaseContainer
can't implement the sealed class Container
.kotlinforandroid
01/23/2024, 4:47 PMimport kotlin.coroutines.CoroutineContext
abstract class ResultUseCase<in P, out R> {
protected abstract val coroutineContext: CoroutineContext
abstract suspend fun doWork(param: P): R
suspend operator fun invoke(param: P): R = doWork(param)
}
When I have an implementation that has no parameters that it needs, I used Unit
as the I
generic. However, when invoking doWork
or using the invoke operator I have to specifically pass Unit
to it. Is there a way to not have to pass Unit
to it? Maybe some other generic or so?Alex
01/24/2024, 8:14 AMthis
of the context receiver (instead of ClassA) to inspect its class attributes? I tried this@foo::class.java
but there is no way to force the this
to be of type T
(context receiver)?
context(T)
suspend fun <T : Any> ClassA.foo(parameter: ParameterA): Result<Unit> {
.. how do I get the instance of T here to inspect its class attributes?
}
Rob Elliot
01/24/2024, 10:05 AMDuration
does for time durations?
I'm imagining:
val maxSize: MemorySize = 2.gigabytes
assert(maxSize.bytes == 2_147_483_648L)
assert(maxSize.toString() == "2GB")
or something like thatjuliocbcotta
01/27/2024, 12:29 PMSven Wollinger
02/01/2024, 10:54 AMfun exec(command: String, directory: File) {
//logger.debug("exec(command=$command, directory=$directory)")
val process = ProcessBuilder()
.redirectErrorStream(true)
.command(command.split(" "))
.directory(directory)
.start()
process.inputReader().lines().forEach {
println("Log: $it")
}
val result = process.waitFor()
println("Done!")
if(result != 0) throw Exception("Return code: $result")
}
"Done!" runs before the inputReaders log is finished.
I understand its probably async, but isnt waitFor() supposed to..well, wait?Sven Wollinger
02/01/2024, 10:54 AMSven Wollinger
02/01/2024, 10:54 AMSven Wollinger
02/01/2024, 10:57 AMplantleaf
02/12/2024, 9:33 AMtype MyFunctionType = () => void
interface MyInterface {
foo: MyFunctionType
}
plantleaf
02/12/2024, 9:35 AMfitermay
02/15/2024, 11:36 PMChristopher Hübner
03/12/2024, 8:20 AMprivate inline fun String.isValidFile() = File(this).exists() && File(this).isFile
Luca Nicoletti
03/14/2024, 9:47 AMAds
section?