O
Oliver Wong
Dimitri Maziuk said:Oliver Wong sez:
Yes, precisely. I don't know the type of E in "class Foo<ArrayList<E>>"
and that's what I want to tell the compiler.
That's like saying "I don't know the type of E in 'fsdljfskdjfsdf'". What
you wrote is not legal Java, and so doesn't make sense. I'm guessing what
you MEANT to say is something like this:
<paraphrasing>
I don't know the type of E in:
interface Foo [don't know what to write here] {
public ArrayList<E> getArray();
}
</paraphrasing>
And I'd answer:
<answer>
Okay, so you need to write code like this:
interface Foo<E> {
public ArrayList<E> getArray();
}
</answer>
If you protest "But Foo is not a collection of E!", I would reply "Foo
isn't a collection at all; but the unknown type is E, and that's what why
you write Foo<E>. 'Cause it's E that's unknown, not ArrayList which is
unknown."
And I can get it to compile with
class Foo<T, C extends ArrayList<T>>
Except it won't actually do what generics are supposed to: a
Foo<String, ArrayList<String>> will happily take an Integer due
to RTTE.
I'm not sure what YOU think generics is supposed to do, but probably
"Class Foo<T, C extends ArrayList<T>>" does exactly what it says it does
(unless there's a bug in your compiler).
When you say "Foo<String, ArrayList<String>> will happily take an
Integer", maybe you could post an SSCCE showing where exactly this Integer
will show up, and I can try to show you how to rewrite the code to disallow
this Integer from showing up there.
First of all, if that were true I'd get a "Cannot resolve symbol
Iterable".
No, if it were true, you'd get a program which compiled fine, but in
which name shadowing occured. It shouldn't say "Cannot resolve symbol
Iterable", because you DID declare the Iterable symbol.
Secondly, the compiler will let me use that field in foreach loop (sans
the cast to Object thanks to RTTE). Finally, if I replace ArrayList with
Iterable in my Foo, it'll actually run just as nicely (FVO "nicely" =
"casting Strings to Objects) as before.
So it's pretty safe to suggest that you think again (dunno about Chris).
From reading Chris' reply to your post elsewhere in the thread, I'm
fairly confident I had guessed correctly at what Chris meant.
As for your other question, both. As in
Table< ROWS extends List< ROW extends List< CELLSTORAGE > >,
COLS extends List< CELLTYPE > >
What's "CELLSTORAGE" in this case? Would this be the same as CELLTYPE?
- Oliver