More like a type puzzle, is it possible to solve i...
# announcements
g
More like a type puzzle, is it possible to solve it without specifying the second type as well?
Copy code
interface IFoo<A>
class Foo: IFoo<String>
class Bar<A>: IFoo<A>

inline fun <reified T: IFoo<*>> createBar(){
   val type = T::class.supertypes[0].arguments[0].type // the type is not lost, String is here

   Bar<type>() // ?? how to use it here
}

createBar<Foo>()
I know that I can declare something like
fun <A, T:IFoo<A> createBar()
, but it's kind of repeating myself and the
A
is already known...
1
a
What's the problem with
Bar<T>()
?
i.e. what's the point of using reflection?
d
You need an unchecked cast here.
Using the second type parameter would be a lot prettier
g
@Dico how is cast going to help to create
Bar<type>
? I mean, this line doesn't even compile 🙂
d
Well, there's no point having the type at all. Generics are erased at runtime, yet you're trying to use them at runtime 😛 Unless something is missing from your example?
g
But it's not erased in this case, the
type
variable holds the exact generic argument, which is
String
here. So, it's more like a theorethical question, if it's possible to pass it to
Bar<here>()
or not.
d
I think you know the answer
Haha
Merry Christmas