Skip to main content
The 2024 Developer Survey results are live! See the results

All Questions

Tagged with
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 ...
The Zip Creator's user avatar
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 ...
SophisticatedThings's user avatar
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 ...
lolo.io's user avatar
  • 772
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(&...
user14381362's user avatar
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 : ...
Abbas Gadhia's user avatar
  • 14.9k
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 ...
Mandarin Smell's user avatar
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. ...
K0cur's user avatar
  • 120
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: ...
K0cur's user avatar
  • 120
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 ...
Boris's user avatar
  • 1,116
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 ...
Илья Жиленков's user avatar
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 { ...
phi oa's user avatar
  • 3
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 ...
Werner Thumann's user avatar
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 ...
Basile Lamarque's user avatar
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 ...
ROUISSI Mohamed ALi's user avatar
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....
user24962484's user avatar

15 30 50 per page
1
2 3 4 5
103