Bug in DataType#isCompatible(Class<? extends DataValue&gt

Hello,

I think the check at line 987 in DataType class's source should be
if (cl.isAssignableFrom(valueClass)) ... .
In the current situation the IntCell is not compatible with IntCells, which is quite sad.
I use KNIME 1.3.5.
Thanks..

Hi,

the code is correct (I believe). The problem may be that you invoke the method with a cell class as argument (as opposed to use the DataValue). There is a long story behind the DataCell classes (e.g. IntCell) and DataValue interfaces (e.g. IntValue). The values are the interfaces, which you should use throughout your code (for instance to do type casts to) and the cells are the actual implementations (you should only need a constructor, if you ever call methods on DataCells or do type casts, you very likely do something you are not supposed to do - use the DataValue instead).

Anyway, to clarify the proper use of the DataType#isCompatible method, see the following code:

// trivial case (works!)
assert IntCell.TYPE.isCompatible(IntValue.class);

// this reads as: You can safely type cast IntCells to DoubleValue
assert IntCell.TYPE.isCompatible(DoubleValue.class);
// … but you can’t cast DoubleCells to IntValue
assert !DoubleCell.TYPE.isCompatible(IntValue.class);

// code like this unfortunately compiles (as the method signature
// requires arguments of Class<? extends DataValue>, and DataCells
// typically implement various DataValue interfaces). The assertion
// will fail, however, as you do the check based on cells, not on
// DataValues (as above).
assert IntCell.TYPE.isCompatible(IntCell.class);

This appears to be difficult at first sight but distinguishing between cells and values pays off (in particular if you have more complex types than just ints).

I'll see if I can issue a warning message if the method is called with a DataCell as argument class.

Let us know if you need more help.

Bernd