On Monday 10 November 2008 08:14, HamletDRC wrote:
> I get a compile error in Groovy 1.5.6 and I think it is a problem
> with type erasure. Does anyone know if I'm doing something wrong
> here? Or can I not do this in Groovy?
>
> (Apologies in advance if the code formatting is bad)
Everyone's code formatting is bad except mine, of course...
> I have a Groovy implementation of a Java interface which fails to
> compile. The Java interface in turn extends two other Java
> interfaces, both of which have generic types.
>
> If I define everything in Groovy then it works... I have a Validator
> and Transformer interface, and a ValidatingTransformer interface
> which extends them both:
>
> interface Validator <T, U> {
> U isValid(T target);
> }
>
> interface Transformer<T, U> {
> U transform(T target);
> }
>
> interface ValidatingTransformer<T, U, V> extends Validator<T, U>,
> Transformer<T, V> {
> }
>
> So long as these interfaces are all defined in Groovy, then I can
> implement the ValidatingTransformer with a Groovy object and the
> object compiles:
>
> class ValidatingTransformerImpl implements
> ValidatingTransformer<String, Boolean, Integer> {
> public Boolean isValid(String target) {
> return null // do some validation
> }
>
> public Integer transform(String target) {
> return null // do some transformation
> }
> }
>
> However, if I define Validator, Transformer, and
> ValidatingTransformer as Java objects, then the
> ValidatingTransformerImpl fails to compile with the following error
> message: "Can't have an abstract method in a non-abstract class. The
> class 'ValidatingTransformerImpl' must be declared abstract or the
> method 'java.lang.Object transorm(java.lang.Object)' must be
> implemented", and a similar message for the isValid method.
I believe the problem is a mismatch of your template vs. actual types:
T: String
U: Boolean
V: Integer
Both isValid() and transform() must return U (Boolean in your
implementing class), but your actual method definitions have one
returning Boolean and the other returning Integer.
> ...
>
> Thanks in advance,
> Hamlet D'Arcy
Randall Schulz
---------------------------------------------------------------------
To unsubscribe from this list, please visit:
http://xircles.codehaus.org/manage_email