Im struggling a bit with reified generics. I have ...
# announcements
m
Im struggling a bit with reified generics. I have a class that needs to call a 3rd party inline function. I want a method in my class to be generic and pass the generic type to the inline function but i cannot do this:
Copy code
class MyClass {
        suspend fun <T> doStuff(): T {
            return myReified<T>()
        }
    }

suspend inline fun <reified T> myReified(): T
What is a good workaround?
d
Your own method needs to be
inline
and the
T
needs to be
reified
itself.
m
I cannot do this, i want
doStuff()
to be public and access class members.
Public-API inline function cannot access non-public-API
d
That's the only way to call a function with a reified parameter, your own parameter needs to be reified itself. You can encapsulate it like this though: https://pl.kotl.in/QLG5ELkp9
doStuffImpl
can access privates, because its not inline
You can even mark
doStuffImpl
as
@PublishedApi
and
internal
if you don't want it called otherwise
m
oh thats an interesting solution, let me play around with that. Thank you