How to call an abstract method from a Class parameter in Kotlin? - kotlin

Aim
Have a function Book, which takes one of three Letter classes as argument myClass and then calls 'genericMethod()' from the abstract class which Letter*() has inherited.
Issue
If I try Book(LetterA()).read() I get the following error:
Type mismatch. Required: Class<SampleClassArguments.Alphabet> Found: SampleClassArguments.LetterA
Does Kotlin have any way to achieve this result?
Code
#Test
fun readBookTest() {
Book(LetterA()).read() /*<--error here*/
}
class Book(val myClass: Class<Alphabet>) {
fun read() {
val letterClass = myClass.getConstructor().newInstance()
letterClass.genericMethod(myClass.name)
}
}
class LetterA(): Alphabet()
class LetterB(): Alphabet()
class LetterC(): Alphabet()
abstract class Alphabet {
fun genericMethod(className: String) {
println("The class is: $className")
}
}

You need to define the Class type as covariant with the out keyword so any of the child classes is an acceptable argument:
class Book(val myClass: Class<out Alphabet>)
And when you use it, you need to pass the actual Class, not an instance of the class. You can get the Class by calling ::class.java on the name of the class:
#Test
fun readBookTest() {
Book(LetterA::class.java).read()
}

Related

Override generic method in abstract class with concrete type in subclass

I have an main class in Kotlin which defines one concrete and one abstract generic method as follows:
abstract class MainClass {
abstract fun <TParent, TChild> getChildren(parent: TParent): Array<TChild>?
fun <TParent, TChild> processChildren(parent: TParent) {
val children = getChildren<TParent, TChild>(parent)
// ... do something with children
}
}
As you can see the method is about a parent object of type TParent containing child objects of type TChild. The parent class and how to get the children from it should be defined by subclasses, which should provide the concrete types.
I did the following:
class MyClass : MainClass{
override fun getChildren(parent: MyParent): Array<MyChild>? {
//... some logic getting the children from a parent object
}
}
But that does not work because it won't compile ('getChildren' overrides nothing).
How can I define a generic abstract method and implement it with concrete types in a subclass? Note that it is important to me that the generic types are defined on method-level, not on class level!
You cannot do this. Due to the type erasure the methods signatures will not fit.
What you actually can do is to make this abstract method protected and overload it with some specific types
// protected abstract fun <TParent, TChild> getChildren...
class MyClass: MainClass() {
override fun <TParent, TChild> getChildren(parent: TParent): ArrayList<MyChild>? {
// Some dummy implementation
println(parent!!::class.java.canonicalName)
return null
}
fun getChildren(parent: MyParent): ArrayList<MyChild>? {
return getChildren<MyParent, MyChild>(parent)
}
fun getChildren(parent: MyParent2): ArrayList<MyChild2>? {
return getChildren<MyParent2, MyChild2>(parent)
}
}
But not really sure what is the sense of doing this especially if generic types are not bounded
Looks like you don't want the method in one class to process different types of parameters.
In this scenario, make the class generic:
abstract <TParent, TChild> class MainClass {
abstract fun getChildren(parent: TParent): Array<TChild>?
fun processChildren(parent: TParent) {
val children = getChildren<TParent, TChild>(parent)
// ... do something with children
}
}
class MyClass : MainClass<MyParent, MyChild>() {
override fun getChildren(parent: MyParent): Array<MyChild>? {
//... some logic getting the children from a parent object
}
}

How to create a class with a method that returns a subtype with a type parameter in Kotlin?

I am struggling to understand how generics / type parameters work in Kotlin. I am working on a (fairly complex) app that is throwing some very confusing error messages during compilation. I've simplified things below to the minimum amount of code that will reproduce the error.
I have an interface and two abstract classes:
interface Player {
fun play()
}
abstract class Device <T : Player> {
abstract fun getPlayer(): T
}
abstract class DeviceFactory {
abstract fun <T : Player> create(): Device<T>
}
The problem arises when I try to create a class that implements DeviceFactory:
class MyDeviceFactory : DeviceFactory() {
class MyPlayer : Player {
override fun play() {
println("[sound plays here]")
}
}
class MyDevice : Device<MyPlayer>() {
override fun getPlayer() = MyPlayer()
}
override fun create() = MyDevice()
}
The last line of code is where the problem arises, yielding the following error message:
Conflicting overloads: public open fun create(): MyDeviceFactory.MyDevice defined in MyDeviceFactory,
public abstract fun create(): Device defined in DeviceFactory
Thinking that maybe the problem was the missing type parameter, I tried this instead:
override fun <T : Player> create() = MyDevice()
Now I have a different error message:
Return type of 'create' is not a subtype of the return type of the overridden member
'public abstract fun create(): Device defined in DeviceFactory'
This doesn't make sense — MyDevice is a subtype of Device<T>, right? To be sure, I tried making the return type explicit:
override fun <T : Player> create(): Device<T> = MyDevice()
No dice:
Type mismatch: inferred type is MyDeviceFactory.MyDevice but Device was expected
How can I create a class that derives from DeviceFactory and returns an instance of MyDevice?
You need to declare the type for DeviceFactory on it's class:
abstract class DeviceFactory<T : Player> {
abstract fun create(): Device<T>
}
Then you can define a factory that returns a concrete Player:
class MyDeviceFactory : DeviceFactory<MyPlayer>() {
override fun create(): Device<MyPlayer> = MyDevice()
}

Calling an overloaded method with the base class parameter type

Is it possible in Kotlin to call an overloaded method using the base class type as a parameter? This is best illustrated via an example
Base Sealed Class + Derived Classes
sealed class Event {
abstract val eventId: String
}
data class FirstEvent(
override val eventId: String
val first: String
) : Event()
data class SecondEvent(
override val eventId: String
val second: String
) : Event()
Utility Class having an overloaded method for each of the derived classes
class UtilityClass {
fun handle(event: FirstEvent) {
....
}
fun handle(event: SecondEvent) {
....
}
}
Is it possible to call methods of the utility class in such a way utility.handle(FirstEvent("id", "first) as Event) doing so is giving me the following exception
None of the following functions can be called with the arguments supplied.
you can do something like this
fun handleEvent(event: Event) {
when (event) {
is FirstEvent -> {
// event is automatically casted as FirstEvent
event.first
}
is SecondEvent -> ...
}
}

Cannot call a function from a child type from a constructor

Is it possible to call a function from a child type from a constructor? Please take a look at the example
class Dog(animalType: DogType) : Animal(animalType) {
fun doSomething() {
animalType.runDogTypeFunction() // error but animalType is always DogType
}
}
abstract class Animal(val animalType: AnimalType)
interface AnimalType
enum class DogType() : AnimalType {
DOG1, DOG2;
fun runDogTypeFunction() {}
}
enum class CatType() : AnimalType {
CAT1, CAT2;
fun runCatTypeFunction() {}
}
animalType is const (val) so it always is DogType. I do not understand why I cannot call a method from the DogType class.
I tried to override val but I received NPE
Your property is declared in the base Animal class (as AnimalType). The constructor param in Dog doesn't exist by the time you call doSomething.
You could try something like this:
abstract class Animal<T: AnimalType>(val animalType: T)
class Dog(animalType: DogType) : Animal<DogType>(animalType) {
fun doSomething() {
animalType.runDogTypeFunction()
}
}

Kotlin automatic derived class constructor with signature matching super class constructor

Sorry that I do not have practical example for such thing, but I am thinking about such feature from time to time. Here is my synthetic example:
abstract class Greeter(val firstName: String, val lastName: String) {
abstract fun greet()
}
class Formal(firstName: String, lastName: String): Greeter(firstName, lastName) {
override fun greet() {
println("Hello $firstName $lastName!")
}
}
class Informal(firstName: String, lastName: String): Greeter(firstName, lastName) {
override fun greet() {
println("Hi $firstName!")
}
}
Here super class constructor is called explicitly by derived classes. Is it possible to do it automatically? The desired thing is to be able do something like:
class Informal(*): Greeter(*) {
override fun greet() {
println("Hi $firstName!")
}
}
Which will generate derived class constructor with same signature as super class (primary) constructor and that constructor will just call super class (primary) constructor.
For me it is not a big problem to define derived class for the first time, but changing all derived classes when base class constructor is changed could be annoying.
As of Kotlin 1.2.x, this is not possible. There is an open feature request for this functionality which is under consideration for future Kotlin versions.