I am trying to write a generic scala utility function for dealing with Apache Thrift generated Java classes. All Thrift generated Java classes extend the TBase
interface with the following signature:
public interface TBase<T extends TBase<?,?>, F extends TFieldIdEnum>
This definitely has always proved to be problematic when making generic thrift utility functions and in the Java world I have typically solved this by just using @SuppressWarnings("rawtypes")
knowing that I was dealing with it correctly and effectively ignoring the compiler telling me it can't verify the matching types.
In the scala world, I don't seem to have the luxury or ignoring these types and have run into a situation where I can't seem to figure out how to satisfy the generic type requirements.
So I have created my own function which converts the given non-thrift object to an instance of the requested Thrift class with a signature similar to:
def myFunc[T <: TBase[T, E], E<: TFieldIdEnum](obj: Any, clazz: Class[T]): T = {
So my issue here is that to call this function I have to do something like:
myFunc[MyThriftClass,MyThriftClass._Fields]( obj, classOf[MyThriftClass] )
Without the second type parameter explicitly specified, the compilation fails with:
error: inferred type arguments [MyThriftClass,Nothing] do not conform to method myFunc's type parameter bounds [T <: org.apache.thrift.TBase[T,E],E <: org.apache.thrift.TFieldIdEnum]
myFunc( null, classOf[MyThriftClass])
^
error: type mismatch;
found : Class[MyThriftClass](classOf[MyThriftClass])
required: Class[T]
myFunc( null, classOf[MyThriftClass])
When I pass both type parameters, everything works though I would prefer to only need to pass the class.
But what I can't seem to figure out how to get this to work if I don't know the types at compile time.
For example, thrift metadata is defined via FieldValueMetaData
objects and when there's a sub-struct, the provided metadata object is StructMetaData
which contains a field
public final Class<? extends TBase> structClass;
Given that this class is missing the type parameters, I don't know how to call my method with it while satisfying the generic type constraints. In the case of recursion I managed to bypass this by casting the substruct class to an instance of Class[T]
which is not true because it's the parent type not the sub-type but due to type erasure it makes the compiler happy and works at runtime. But if I don't already have a generic type to cast to I don't know how to handle this aside from maybe a really hacky approach where I cast it to some fake shell of a TBase class just to make the compiler happy.
Is there a proper way to set this up to make the compiler happy with the real types? Alternatively is there a way to bypass this similarly to how I would in pure Java with @SuppressWarnings("rawtypes")
. Otherwise if not then maybe I'll just have to port this logic over to Java and call it from Scala to make everything happy. Before doing this though I'd either like to learn what I'm missing or learn why what I want is not possible.
Edit:
Really what I'm looking for is some way in scala to call my existing myFunc method given a Tbase[_,_]
. In Java I'd do this with suppressing raw types but in scala I'd like to either know a way to satisfy the types and allow my method to be called or a way to suppress them and allow my method to be called.
Edit 2:
Figured out a way to do what I want but still looking for cleaner solutions if they exist. What I ended up with is the following:
def myFunc[T <: TBase[_,_],T2 <: TBase[T2, E2], E2<: TFieldIdEnum](obj: Any, clazz: Class[T]): T = {
_myFunc[T2,E2]( obj, clazz.asInstanceOf[Class[T2]] ).asInstanceOf[T]
}
def _myFunc[T <: TBase[T, E], E<: TFieldIdEnum](obj: Any, clazz: Class[T]): T = {
To be honest I don't know why it works because I haven't specified what types T2
or E2
are but this does seem to make it compile and run.
def myFunc[T <: TBase[T, _ <: TFieldIdEnum]](obj: Any, clazz: Class[T]): T
doesn't work for you? But it's hard to know exactly why, or to guess what would work, because your actual problem appears to be hiding somewhere in the code you are not showing.E
so it can't simply be a wildcard. Thrift classes have methodssetFieldValue
andgetFieldValue
that take an object of typeE
as an argument. Unfortunately to dynamic get theTFieldIdEnum
s of the object, I need to use reflection as this isn't something exposed with an external generically typed method. Since I'm using reflection I actually get an object of typeTFieldIdEnum
instead ofE
and therefore I need the typeE
for casting purposes or else it won't let me call these methods.