IntegerType IntType ImgLib2




@imagejan @wolny @haesleinhuepf

I have an RandomAccessibleInterval rai which happens to be of UnsignedShortType, but I would like to keep the code generic and just specify functions to just require a RandomAccessibleInterval< IntType >. This compiles well, but once I start iterating over the rai within a function, for example like this:

for ( final IntType input : Views.flatIterable( rai ) )

I get a runtime error:

java.lang.ClassCastException: net.imglib2.type.numeric.integer.UnsignedShortType cannot be cast to net.imglib2.type.numeric.integer.IntType

Related to that, could someone maybe explain how I could figure out what the relation of UnsignedShortType to IntType is in terms of Class hierarchies?

Sorry, but I cannot not figure it out alone and am super helpful for any advice.


Hey @Christian_Tischer,

Easy answer here, I hope. Basically, just change <IntType> to <IntegerType>
IntegerType is the interface that both UnsignedByteType and IntType implement.

Btw, depending on your exact situation you could change RandomAccessibleInterval<IntegerType> to one of these:

	public static <T extends IntegerType<T> > void foo( T t )
		RandomAccessibleInterval< ? extends IntegerType<?>> a;
		RandomAccessibleInterval< T > b;

Hope that does the trick,


Hi @Christian_Tischer

If you click on the class name UnsignedShortType in IntelliJ and press CTRL+B (CMD+B on mac) you go to the class definition. There, you will learn that it extends GenericShortType. If you move your cursor on this one and press the short cut again, you will learn that it extends AbstractIntegerType. In the same way, you will learn that IntType extends GenericIntType which also extends AbstractIntegerType. In order to understand the concept in detail, I would recommend reading this paper and especially the supplement. There you also find figure 3 visualising the class hierarchy:


P.S.: Yes, I also wish this all would be simpler. I hope some day we find a way to simplify it.


Thanks, that does the trick!!

Simply using IntegerType also works during run-time, but only if I do a raw cast of the input rai, otherwise I get a compile-time error:

Error:(135, 128) java: incompatible types: inferred type does not conform to equality constraint(s)
    inferred: net.imglib2.type.numeric.IntegerType
    equality constraints(s): net.imglib2.type.numeric.IntegerType,net.imglib2.type.numeric.integer.IntType

If I remember right from a recent Java course, these kind of compile time errors are not exactly logical, is it?
Because the code in fact runs just fine and because IntType is in fact an IntegerType. Or am I missing something here?