Questions tagged [kotlin-generics]

18 questions
10
votes
2 answers

Kotlin Generics Error in Java

Given the following three kotlin classes: abstract class UseCase { fun execute(action: Action) { } } class ConcreteUseCase : UseCase>() class Action I am unable to compile following lines in java…
M-Wajeeh
  • 17,204
  • 10
  • 66
  • 103
5
votes
2 answers

Generic function in interface to return concrete implementation

To an interface, I want to add a function that returns an object of the concrete implementation. So with the interface: interface Content { fun cloneMe(): ? } and the classes class Music: Content class Video: Content the function…
fweigl
  • 21,278
  • 20
  • 114
  • 205
5
votes
2 answers

Kotlin override fun with subtype

Im having trouble inheriting an interface containing a method/fun of a base type, that i would like to override as a subtype in the class implementing it. So far i have the interface interface IModel { fun convert(dataModel:…
nsL
  • 3,722
  • 3
  • 23
  • 40
4
votes
1 answer

kotlin generics for Consumer only

Let' say i have this java example: interface Sink { void accumulate(T t); } public static void drainToSink(Collection collection, Sink sink) { collection.forEach(sink::accumulate); } Notice how the second parameter is…
Eugene
  • 117,005
  • 15
  • 201
  • 306
3
votes
1 answer

Passing only types marked as reified to Kotlin generic function

Let's say I have the following code: open class Fruit class Apple : Fruit() open class Juice class AppleJuice : Juice() fun > makeJuice(juiceClass : Class, fruit : F) : J {} I call the function like…
3
votes
2 answers

Inferring parameter of class type with default

I have a class with constructor declared like this class Facade( val kClass: KClass = SuperClass::class ) It is done like this so that developer doesn't have to specify SuperClass if they want to use it instead of a…
Majkeee
  • 960
  • 1
  • 8
  • 28
3
votes
1 answer

Kotlin "out" and "in" and generics - proper usage

I was trying to make a generic poor-man's data persistence function that would take a MutableSet of data class and serialize it to disk. I'd like something easy for prototyping, and am OK calling "save()" on the set every so often so that if my…
Benjamin H
  • 5,164
  • 6
  • 34
  • 42
2
votes
3 answers

Why can't Kotlin smart cast between an interface and the generic type that is derived from it?

I have the following class: abstract class PresenterActivity : AppCompatActivity() { open fun initViewIntent(): I { return object : ViewIntent{} // type mismatch on this line } } I receive a…
S-K'
  • 3,188
  • 8
  • 37
  • 50
2
votes
1 answer

Kotlin generic properties issue

I got some issues with Kotlin when translating my android project from java to Kotlin. Say i have interface I and interface O which extends interface I. interface I{ } interface O: I{ } And generic class A which have generic parameter V that…
Vlad Morzhanov
  • 1,260
  • 3
  • 14
  • 29
1
vote
1 answer

why the translated kotlin code complains about a Array? to be a Array

Having a java class, using androidStudio to translate to kotlin. Got a error and not sure how to correctly translate it. The java code: public class BaseDataImpl extends BaseData { private final BaseData[] translators; public…
lannyf
  • 9,865
  • 12
  • 70
  • 152
1
vote
5 answers

(kotlin) editText.toString().toInt() isn't work in anroid studio

val editText1 = findViewById(R.id.editText1); if(comNum != editText1.toString().toInt() ){ View4.text = "오답" } else View4.text = "정답" The installed apk is not working. I think edittext.toString.toInt is wrong.
유재언
  • 21
  • 3
1
vote
1 answer

Why can't make to methods with same name but with different generics?

I'm pretty new in Kotlin language, but I have just encountered some strange behavior that didn't have in other languages, so I wanted to ask why I can't do something like this: fun methodName() { // whatev~ } fun methodName() { …
Patroklo
  • 516
  • 4
  • 15
1
vote
1 answer

How to have a class with a self type paramter as interface in kotlin

I have a class Element with a Self type parameter interface Element> { val rules: Set<(Self) -> Boolean> } How can I now create a List with Element as type parameter because the following doesn't function of course. val…
nikolausk
  • 31
  • 2
1
vote
0 answers

Kotlin generic issue, should be subtype of Nothing

I'm writing a simple Spring test on Kotlin and have a compilation error with generics that I can't understand. The function is below: @Test fun actuatorRootReturnsOnlyAllowed() { val expectBody:WebTestClient.BodySpec
yyunikov
  • 5,719
  • 2
  • 43
  • 78
0
votes
1 answer

Can I extend Comparable in Kotlin?

I wanted to implement min()/max() aliases for the Kotlin's ComparablecoerceAtLeast()/coerceAtMost(), if nothing else for the exercise of extending an Interface (I've only extended classes so far). I tried this: fun Comparable.max(other:T) :…
Travis Griggs
  • 21,522
  • 19
  • 91
  • 167
1
2