I have a java library with some generic containers:
public interface IColumnTable<T extends IColumn<?, ?>>
{
}
public interface IColumn<D extends IColumnValues<?>, M extends IMetaData>
{
}
public interface IColumnValues<E>
{
}
public interface IMetaData
{
}
and a factory method to get concrete instances of them
public interface StorageFactory
{
IColumnTable<? extends IColumn<? extends IColumnValues<?>, ? extends IMetaData>> read(String tableName) throws IOException;
}
And I also have a utility method that types my wildcard tables (through casting and converting of values)
public class TableConverterUtil
{
public static <T, V> IColumnTable<IColumn<IColumnValues<T>, IMetaData>> getPureTypedTable(
IColumnTable<? extends IColumn<? extends IColumnValues<V>, ? extends IMetaData>> tableRaw,
Class<T> type,
Optional<Function<V, Optional<T>>> converter
)
}
(Note the use of V instead of ? as the parameter for IColumnValues)
In java I can call the following to get a table with values of Double:
try {
IColumnTable<IColumn<IColumnValuesExact<Double>, IMetaData>> myDoubleTable = TableConverterUtil.getPureTypedTable(
StorageManagerUtil.getDefault().get("default").read("myTableName"),
Double.class,
Optional.empty()
);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (StorageManagerInstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
However in Scala I don't seem to be able to get the same call to work:
TableConverterUtil.getPureTypedTable(
StorageManagerUtil.getDefault.get("default").read("myTableName"),
classOf[Double],
Optional.empty()
)
Results in:
[file and line number]: no type parameters for method emp
ty: ()java.util.Optional[T] exist so that it can be applied to arguments ()
[error] --- because ---
[error] undetermined type
[error] Optional.empty()
[error] ^
[error] [file and line number]: type mismatch;
[error] found : java.util.Optional[T]
[error] required: java.util.Optional[java.util.function.Function[?,java.util.Op
tional[?]]]
[error] Optional.empty()
[error]
^
I have also tried it with null instead of Optional.empty() which results in:
[error] [file & line no]: no type parameters for method get
PureTypedTable: (x$1: com.wwa.data.interfaces.IColumnTable[_ <: com.wwa.data.int
erfaces.IColumn[_ <: com.wwa.data.interfaces.IColumnValues[V], _ <: com.wwa.data
.interfaces.IMetaData]], x$2: Class[T], x$3: java.util.Optional[java.util.functi
on.Function[V,java.util.Optional[T]]])com.wwa.data.interfaces.IColumnTable[com.w
wa.data.interfaces.IColumn[com.wwa.data.interfaces.IColumnValuesExact[T],com.wwa
.data.interfaces.IMetaData]] exist so that it can be applied to arguments (com.w
wa.data.interfaces.IColumnTable[?0], Class[Double], Null)
[error] --- because ---
[error] argument expression's type is not compatible with formal parameter type;
[error] found : com.wwa.data.interfaces.IColumnTable[?0(in method doWork)] wh
ere type ?0(in method doWork) <: com.wwa.data.interfaces.IColumn[_ <: com.wwa.da
ta.interfaces.IColumnValues[_], _ <: com.wwa.data.interfaces.IMetaData]
[error] required: com.wwa.data.interfaces.IColumnTable[_ <: com.wwa.data.interf
aces.IColumn[_ <: com.wwa.data.interfaces.IColumnValues[?V], _ <: com.wwa.data.i
nterfaces.IMetaData]]
[error] TableConverterUtil.getPureTypedTable(
[error] ^
[error] [file & line No.]: type mismatch;
[error] found : com.wwa.data.interfaces.IColumnTable[?0(in method doWork)] wh
ere type ?0(in method doWork) <: com.wwa.data.interfaces.IColumn[_ <: com.wwa.da
ta.interfaces.IColumnValues[_], _ <: com.wwa.data.interfaces.IMetaData]
[error] required: com.wwa.data.interfaces.IColumnTable[_ <: com.wwa.data.interf
aces.IColumn[_ <: com.wwa.data.interfaces.IColumnValues[V], _ <: com.wwa.data.in
terfaces.IMetaData]]
[error] StorageManagerUtil.getDefault.get("default").read("myTableName"),
[error]
^
I have also tried an auxiliary method to trap the wildcard given to IColumnValues so that I can actually pass a function in. But no luck, it seems that whenever I try and bind one of the inner _ to a named type parameters the compiler throws a fit.
I can at a stretch change the Java library but think I must be missing something as Scala and Java should be totally interoperable.
Is there a way to call the getPureTypedTable from scala without changing the java? (with a converter function not null or empty optional)
PS: Sorry for long winded post its late on Friday and I've been staring at this for a while now
Think I found the answer in this scala group thread https://groups.google.com/forum/#!topic/scala-user/JlCsy48poIU
It involves pattern matching to name the type but also gives a compiler warning about unchecked types which I think is safe to ignore...
def callWildcardTableFunction(table: IColumnTable[_ <: IColumn[_ <: IColumnValues[_], _ <: IMetaData]])
: IColumnTable[IColumn[IColumnValues[Double], IMetaData]] = table match
{
case boundTable: IColumnTable[IColumn[IColumnValues[valueType], m]] @unchecked =>
TableConverterUtil.getPureTypedTable[Double, valueType](boundTable, classOf[Double], null)
}
If anyone has a better way / this is actually not safe please say!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.