Better generic parameter type matching
- Dominant language
- Java
- Stars
- 94
- Forks
- 44
- PR merge metrics
- No merged PRs in 30d
Description
When generics are involved, OPS does decent parameter type matching when deciding which op is the most appropriate for a given list of arguments. The reason it is decent, and not just crappy, is thanks to the great gentyref library, which helps tremendously for resolving when an object will actually safely fit into a given parameter field.
However, due to runtime generic type erasure, there are still cases where we cannot figure out whether the match is safe. For example:
```
public class AddConstantToArrayByteImage implements Op {
@Parameter(type = ItemIO.BOTH)
private ArrayImg image;
```
And:
```
public class AddConstantToArrayDoubleImage implements Op {
@Parameter(type = ItemIO.BOTH)
private ArrayImg image;
```
In this case, we cannot distinguish between an argument (i.e., object instance) of type `ArrayImg` and `ArrayImg` because at runtime, all that is known is that the argument is of type `ArrayImg`. But in practice it is backed by a particular type, e.g. `ByteType`, meaning that if it erroneously matches to the `AddConstantToArrayDoubleImage`, there will later be a `ClassCastException` when attempting to work with the object as though it were an `ArrayImg` (in this case, maybe when trying to assign the result of `Cursor#get()` to a `DoubleType`).
The currently proposed solution is to create a `GenericTyped` interface in SciJava Common with method `Type[] getGenericTypes()` that returns a list of `Type` objects matching the generic parameters. Or maybe it should return `Class[]`... further thought and testing is needed. But the idea is to provide generically typed objects with a mechanism to "unerase" their types. Then the OPS matcher can check if the object implements this interface in order to glean its generic types and hence match to the op's inputs more precisely.
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.