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
diesieben07
08/14/2020, 9:05 AM
Your own method needs to be
inline
and the
T
needs to be
reified
itself.
m
Mgj
08/14/2020, 9:05 AM
I cannot do this, i want
doStuff()
to be public and access class members.
Public-API inline function cannot access non-public-API
d
diesieben07
08/14/2020, 9:08 AM
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
diesieben07
08/14/2020, 9:08 AM
doStuffImpl
can access privates, because its not inline
diesieben07
08/14/2020, 9:09 AM
You can even mark
doStuffImpl
as
@PublishedApi
and
internal
if you don't want it called otherwise
m
Mgj
08/14/2020, 9:10 AM
oh thats an interesting solution, let me play around with that. Thank you