0

I am trying to initialize an Object based on Kotlin Type parameters. The implementing class MyClass needs to have zero args constructor as it extends ActionCallback which does not allow parameters.

I have followed the Kotlin guide on inline functions to try and achieve this. The idea is that T is used on MyClass, which is used to instantiate myObject based on the Type. However I get error Cannot use 'T' as reified type parameter. Use a class instead.

Disclaimer: I do not know if inline functions is best way to approach this. I have used this route due to not being able to pass in parameters to MyClass but am open to other approaches.

inline fun <reified T> getObject(): T? {
    return when (T::class) {
        type1::class -> object1 as T?
        type2::class -> object2 as T?
        else -> null
    }
}
class MyClass<T> : ActionCallback {
    var myObject = getObject<T::class>() /*<--Error: Cannot use 'T' as reified type parameter. Use a class instead*/
}
mars8
  • 770
  • 1
  • 11
  • 25
  • What do you mean by "I have used this route due to not being able to pass in parameters to MyClass"? That is kind of suspicious... Can you show what you have tried? – Sweeper Nov 12 '22 at 12:12
  • My example above is an abstract of extending [ActionCallback](https://developer.android.com/reference/kotlin/androidx/glance/appwidget/action/ActionCallback) in Android Glance widgets. As per documentation 'The implementing class must have a public zero argument constructor, this is used to instantiate the class at runtime.' – mars8 Nov 12 '22 at 12:22
  • 1
    So you want `myObject` to be initialised to `getObject()` when a 0-argument constructor is called? Sorry, that's not possible because type erasure. – Sweeper Nov 12 '22 at 12:26
  • 2
    Since it is the Android runtime that is calling the constructor, they are probably doing reflection, and will not physically pass a type parameter anyway. So it does not make sense to say `getObject()` because `T` is undefined. This is likely an [XY problem](http://xyproblem.info/). Please explain why you are trying to create a generic `ActionCallback`, and/or what your ultimate goal is. – Sweeper Nov 12 '22 at 12:35
  • hmmm. It is frustrating because I have to create 4 versions of `MyClass`, with the only difference being a different `myObject` used in each one. The classes have much more code than what I have used in example above. Surely there must be some smart way of having one golden source class with 4 stubs classes that implement it (and swap the object for each one)? – mars8 Nov 12 '22 at 12:39
  • "some smart way of having one golden source class with 4 stubs classes that implement it" Yeah, it's called inheritance, isn't it? Can you [edit] the information you posted in the comments into your question (e.g. change `MyCallback` to `ActionCallback`, make it clear that this is an Android question, etc), so I can edit and undelete my answer? – Sweeper Nov 12 '22 at 12:42
  • @Sweeper I have created a separate [question](https://stackoverflow.com/q/74413298/15597975) with more specifics on the issue. – mars8 Nov 12 '22 at 13:26
  • @Sweeper edited question per your suggestion. – mars8 Nov 12 '22 at 13:44

0 Answers0