Generics problem
Rémi Forax
forax at univ-mlv.fr
Fri Jul 9 07:06:10 PDT 2010
Le 09/07/2010 15:37, Ulf Zibis a écrit :
> See following code:
>
>
> /**
> * @author Ulf Zibis, Cologne CoSoCo.de
> */
> public class GenericsProblem {
>
> private static abstract class Entry<V,E extends Entry<V,E>>
> implements Comparable<E> {
> V value;
> int count = 0;
>
> Entry(V value) {
> this.value = value;
> }
>
> public int compareTo(E other) {
> return count != other.count ? count > other.count ? -1 : 1
> : 0;
> }
> }
>
> private static class Word extends Entry<String,Word> {
>
> private Word(String str) {
> super(str);
> }
> }
>
> private static abstract class Phrase<P extends Phrase<P>> extends
> Entry<int[],P> {
> int prefixLength;
>
> Phrase(int[] codes, int prefixLength) {
> super(codes);
> this.prefixLength = prefixLength;
> }
>
> // <P extends Phrase<P>> int matchDepth(P other) {
> int matchDepth(Phrase<? extends Phrase> other) {
> for (int i=0, l=Math.min(value.length,
> other.value.length); ; )
> if (value[i] != other.value[i] || ++i == l)
> return i;
> }
> }
>
> private static class UName extends Phrase<UName> {
> private final int cp;
>
> UName(int cp, String name) {
> super(wordCodes(name), 1);
> count = 1;
> this.cp = cp;
> }
>
> private static int[] wordCodes(String name) {
> String[] words = name.split("[ \\\\-]");
> int [] codes = new int[words.length];
> for (int i=0; i<words.length; i++)
> codes[i] = words[i].length();
> return codes;
> }
>
> public int compareTo(UName other) {
> int m = matchDepth(other), l1, l2, c1, c2;
> return m == Math.min(l1 = value.length, l2 =
> other.value.length) && l1 != l2 ? l1 < l2 ? -1 : 1 : // (3b) OK!
> (c1=value[m]) != (c2=other.value[m]) ? c1 < c2 ? -1 :
> 1 : 0;
> }
> }
>
> static <V,E extends Entry<V,E>> int sizeOf1(E e) { // number of
> effective elements
> if (e instanceof Word) // compile error ???
> return ((Word)e).value.length();
> else
> return ((Phrase<? extends Phrase>)e).value.length -
> ((Phrase<? extends Phrase>)e).prefixLength + 1;
> // return e instanceof Word ? ((Word)e).value.length() :
> // ((Phrase<? extends Phrase>)e).value.length -
> ((Phrase<? extends Phrase>)e).prefixLength + 1;
> }
> }
>
>
> I get following error:
>
> C:\Projects\Java_Bugs\JDK-7\...\GenericsProblem.java:69: inconvertible
> types
> if (e instanceof Word) // compile error ???
> required: Word
> found: E
> where E,V are type-variables:
> E extends Entry<V,E> declared in method <V,E>sizeOf1(E)
> V extends Object declared in method <V,E>sizeOf1(E)
> C:\Projects\Java_Bugs\JDK-7\...\GenericsProblem.java:70: inconvertible
> types
> C:\Projects\Java_Bugs\JDK-7\...\GenericsProblem.java:70: inconvertible
> types
> return ((Word)e).value.length();
> required: Word
> found: E
> where E,V are type-variables:
> E extends Entry<V,E> declared in method <V,E>sizeOf1(E)
> V extends Object declared in method <V,E>sizeOf1(E)
> 2 errors
>
>
> I don't understand this because:
> E extends Entry<V,E>
> and
> Word extends Entry<String,Word>
>
> Any Ideas?
<V,E extends Entry<V,E>> int sizeOf1(E e) {
means for all V and E ...
clearly e instanceof Word is not valid for all E,
by example, this is invalid if E is a Phrase.
I you declare siseOf1 like this
static int sizeOf1(Entry<?,?> e)
it should be ok.
>
> -Ulf
Rémi
More information about the compiler-dev
mailing list