All Questions
1,534
questions
1
vote
1
answer
24
views
How to use "where" and extend another (generic) class at the same time in Kotlin?
I have two interfaces, Foo and Bar, and I have a class Baz<T : Foo>. I want to create a new class Buz<T> that extends Baz, where Buz's type parameter extends both Foo and Bar. So it'd look ...
0
votes
0
answers
27
views
Is it possible to point dynamic expression after AS operator in Kotlin [duplicate]
I have a problem: kotlin let us make statements like this:
val castedObject = otherObject as CastedObjectClass
and we always hardcode this CastedObjectClass, in other words, statement after the as ...
2
votes
1
answer
38
views
Kotlin, Add an extension function to a class with another receiver type
There is a Kotlin trick I always wanted to achieve but I'm not sure it is possible.
Can we add an extension function to a class, but having another receiver type ?
as an example, let's imagine we want ...
1
vote
2
answers
56
views
How to determine if a class is generic via reflection in Kotlin
Suppose we have the following class:
data class KeyValuePair<T : Any>(
val key: String,
val value: T
)
If we reflect against the following instance of this class: KeyValuePair(&...
0
votes
1
answer
24
views
Kotlin - Filter by generic type parameter of an abstract generified class
I have a generified abstract base class with the bounded type param T: Shape and a collection of type T
abstract class ShapeContainer<T: Shape>(val shapeConsumer: Consumer) {
val shapes : ...
1
vote
1
answer
69
views
How do I guarantee generic to extend specific class safely in Kotlin?
I wanted to create my own Map class which allows only Number class (i.e. Int, Long, Double, ...). Also if value in entry becomes zero, it will automatically remove that entry from map. As I'm not ...
3
votes
1
answer
74
views
Encapsulation of inline reified method in kotlin
I have written the util class to be used with Flow<> in kotlin. The goal of the class is to simplify error handling and make possible to instantly finish the flow by calling breakFlow() method. ...
4
votes
1
answer
54
views
Subclass with generic type parameter declared as non-nullable accepts passing null value
I am learning generics and I have written such a class, but I am not quite sure how it works.
This is the code:
sealed class Result<out T> {
abstract val data: T?
abstract val error: ...
2
votes
0
answers
30
views
Arguments of extension function with receiver of generic type
I would like to replace a set of extension functions like this:
fun Boolean.assertEquals(value: Boolean) = assertEquals(value, this)
fun Int.assertEquals(value: Int) = assertEquals(value, this)
with ...
0
votes
1
answer
33
views
Does Hilt support injection with reified generics?
I want to make my code a bit smaller, and I already tried to do this thing:
@Provides
inline fun <reified T> provideJsonAdapter(moshi: Moshi): JsonAdapter<T> = moshi.adapter()
I already ...
0
votes
1
answer
39
views
Is there any way to achieve static polymorphism in Kotlin?
I've run into a situation, where I got some classes that share the same interface:
sealed interface Animal {
// "const static val nickname: String" ??
}
class Canidae: Animal {
...
2
votes
2
answers
59
views
Kotlin: Handling star projected generics with type tokens
Consider the following generic class which is "reified" by using a type token:
class Gen<X : Any>(val token: KClass<X>) {
fun doSomething(x: X) {
// ...
}
}
Now ...
0
votes
2
answers
96
views
Kotlin generic lambda Type mismatch. Required: nothing
I have a problem using Kotlin generic and lambda.
Basically I want my interface to have a lambda function on a generic type.
My goal is to make multiple implementation of my class A and then use the ...
1
vote
1
answer
190
views
Kotlin 1.9.2, jdk 21 (Spring boot) Jacoco (plugin 0.8.12) is complaining about missing branches
I have run into a jacoco code coverage issue where it complains about missing branches for the kotlin coroutines function withContext(Dispatchers.IO) event though all branches are covered. Nothing ...
0
votes
1
answer
66
views
Calling generic API method from a generic class
I'm relatively new to the Kotlin community and found this apparently simple error that I haven't been able to solve yet:
import com.akuleshov7.ktoml.Toml
import com.akuleshov7.ktoml.source....