Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Eclipse Generics Issue - Workaround?

Given the following type signatures, I'm able to compile and run the code under Maven with both JDK 6 and JDK 7, but Eclipse gives a "Bound mismatch: The type F is not a valid substitute for the bounded parameter <F extends Field<TP,F>> of the type Field<TP,F>" error in TupleVisitor.

I believe I need these types, although I understand this is difficult to motivate given the stripped-down example. Can anyone suggest a workaround that will let me continue to work in Eclipse?

public abstract class Tuple<F extends Field<TP, F>, TP extends Tuple<F, TP>>

public class VariableTuple<F extends Field<VariableTuple<F>, F>> extends Tuple<F, VariableTuple<F>>

public class ConstantTuple<F extends Field<ConstantTuple<F>, F>> extends Tuple<F, ConstantTuple<F>>

public class Field<TP extends Tuple<F, TP>, F extends Field<TP, F>>

public class ConstantField extends Field<ConstantTuple<ConstantField>, ConstantField>

public class VariableField extends Field<VariableTuple<VariableField>, VariableField>

public interface TupleVisitor {
  public <F extends Field<VariableTuple<F>, F>> void visit(VariableTuple<F> tuple, F field); //Eclipse error

  public <F extends Field<ConstantTuple<F>, F>> void visit(ConstantTuple<F> tuple, F field); //Eclipse error
}

Filed bug at: https://bugs.eclipse.org/bugs/show_bug.cgi?id=422503

No simple workaround identified. While Rohit Jain's answer wouldn't work with a visitor pattern, I took his follow-up advice and removed F as a type parameter from Field.

like image 562
Adam Augusta Avatar asked May 25 '26 06:05

Adam Augusta


1 Answers

This seems to a be a bug with eclipse, as it is compiling fine under javac for me too. In fact, there are few bugs related to self-referential Java generics related to eclipse that I found, but this isn't there. So, may be you should file one.

As for a workaround of this, I just found one, which I doubt you would like, as you have to make your interface generic. Yes, you heard it right. Declaring the type parameters with the interface itself, makes the code compile fine. Here's the compiling code:

interface TupleVisitor<E extends Field<VariableTuple<E>, E>, 
                       F extends Field<ConstantTuple<F>, F>>  {
   void visit(VariableTuple<E> tuple, E field);
   void visit(ConstantTuple<F> tuple, F field);
}

Of course there are two type variables, as the bounds on both of them are different. Check if this suits your need, because this really sounds to be as weird work-around, as the number of type parameters depends on the total number of implementors of Tuple class (Actually weird, isn't it?). Or else you have to do some other changes.

like image 158
Rohit Jain Avatar answered May 27 '26 18:05

Rohit Jain



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!