Re: Fun with generics




"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

...You
can'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

.



Relevant Pages

  • Re: how to code to avoid SQL insertion attacks
    ... I intimated that PostgreSQL was brain-dead because it supported ((foo = NULL) ... or NULL and have no truth value referred to as UNKNOWN. ... There are some better quotes on MySQL but no matter. ...
    (comp.lang.java.programmer)
  • Re: a case for multiple inheritance
    ... Although C# has Generics, it still does not support the generic ... I need to expose two lists of objects from a high-level class. ... While this usage of inheritance does not conform to the "is-a" relation ... private new void Foo ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: set default value for a char column??
    ... ever behave more appropriate for "missing value" than NULL does. ... NULL in expressions. ... or False, but also in Unknown. ... WHERE Table1.Column1 NOT IN (SELECT foo FROM bar) ...
    (comp.databases.ms-sqlserver)
  • RE: Difficulty Extending from Generic Base Class
    ... Your not missing anything apart from the diffculty in building generics to ... Bar extend from BaseClass): ... public class Foo: BaseClass ... public override string ToString() ...
    (microsoft.public.dotnet.framework.clr)
  • Re: Fun with generics
    ... description of the purpose of generics. ... so what you need to communicate is the type parameter to ... public interface Table ... using ArrayList is that if someone wants to create a Foo that only works ...
    (comp.lang.java.programmer)