Without using generic types I have the following which works fine:
public class OC_GuillotinePacker implements Iterable<OC_GuillotinePacker> {
@Override
public Iterator<OC_GuillotinePacker> iterator() {
// code that returns a new Iterator
}
}
Which allows me to loop like this:
for (OC_GuillotinePacker gp : packer) {
}
I updated my code so I can store objects by it's generic type. Now it's like:
public class OC_GuillotinePacker<T> implements Iterable<OC_GuillotinePacker<T>> {
@Override
public Iterator<OC_GuillotinePacker<T>> iterator() {
// code that returns a new Iterator
}
}
The problem is when I create a new OC_GuillotinePacker like:
packer = new OC_GuillotinePacker(0, 0, width, height);
Then I can't loop anymore like this:
for (OC_GuillotinePacker gp : packer) {
}
It gives me a incompatible types warning. Requires is OC_GuillotinePacker<> and found is java.lang.Object.
Is it possible to make it so that both is possible? So a raw for each loop and one with a generic type.
When you made your
OC_GuillotinePackerclass generic, with type parameterT, your declaration became raw.When you use a raw type, type erasure occurs, such that the
iterator()method now returns a rawIterator, which returnsObjects. AnObjectcan't be assigned to aOC_GuillotinePacker, hence the error.When creating your
OC_GuillotinePacker, supply a type argument or use the diamond operator to infer the type from thepackervariable declaration.This way, the
iterator()method will returnOC_GuillotinePacker<YourType>objects to use in your enhancedforloop.