Re: Fun with generics
- From: "Oliver Wong" <owong@xxxxxxxxxxxxxx>
- Date: Wed, 22 Mar 2006 23:03:51 GMT
"Dimitri Maziuk" <dima@xxxxxxxxx> wrote in message news:slrne23knu.ksf.dima@xxxxxxxxxxxxxxxxxxxxxxxxxxx
Oliver Wong sez:...
"Dimitri Maziuk" <dima@xxxxxxxxx> wrote in message
news:slrne23cht.kkt.dima@xxxxxxxxxxxxxxxxxxxxxxxxxxx
...Youcan't use a real class as a type parameter -- if that's what you wanted,
you'd just leave out the type parameter and use the real type.No, what he means is you can't do this:
class Foo<ArrayList> {
public ArrayList getMyThing() {...}
}
I.e. you can't specify an "actual class" (where ArrayList is an example of
an actual class), because if you knew ahead of time what actual class you
wanted, you could just put it directly into the code (as seen here in
"public ArrayList getMyThing()". Generics are for when you DON'T know the
type
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.
Chris was subtly saying that the code you provided doesn't mean what you
think it means. You've declared a new generic type variable called
"Iterable", when you probably mean to refer to the existing Iterable.
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
.
- References:
- Fun with generics
- From: Dimitri Maziuk
- Re: Fun with generics
- From: Oliver Wong
- Re: Fun with generics
- From: Oliver Wong
- Re: Fun with generics
- From: Thomas Hawtin
- Re: Fun with generics
- From: Dimitri Maziuk
- Re: Fun with generics
- From: Chris Smith
- Re: Fun with generics
- From: Dimitri Maziuk
- Re: Fun with generics
- From: Oliver Wong
- Re: Fun with generics
- From: Dimitri Maziuk
- Fun with generics
- Prev by Date: Re: Fun with generics
- Next by Date: Re: why java is not pure object oriented?
- Previous by thread: Re: Fun with generics
- Next by thread: Re: Fun with generics
- Index(es):
Relevant Pages
|