Home > Archive > Java Help > November 2007 > Help. Generics notation really <P> me off
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Help. Generics notation really <P> me off
|
|
| Jacob Singer 2007-11-15, 7:58 pm |
| I'm trying to call method query() in class ObjectContainer.
// this is from db4o.com
public interface ObjectSet<Item>
extends java.util.List<Item>, java.util.Iterator<Item>
{
...
}
public class ObjectContainer ...
{
public <TargetType> ObjectSet<TargetType> query(Class<TargetType>
clazz)
throws Db4oIOException, DatabaseClosedException
{
...
}
}
This is my code :
public class MyClass<T extends Shape>
{
private ObjectContainer db = Db4o.openFile(...);
...
public void myMethod()
{
ObjectSet<? extends Shape> os = db.query(T); <-- error here
}
}
The compiler is complaining that "T" in db.query(T) is a variable that
cannot be found. I've never been too clear on the syntax of generics
so I don't understand how the compiler can view T as a variable and
not as a type. Can someone tell me what the parameter should be coded
as please.
ty
| |
| Joshua Cranmer 2007-11-15, 7:58 pm |
| Jacob Singer wrote:
> The compiler is complaining that "T" in db.query(T) is a variable that
> cannot be found. I've never been too clear on the syntax of generics
> so I don't understand how the compiler can view T as a variable and
> not as a type. Can someone tell me what the parameter should be coded
> as please.
T is a type, not a variable. So, in that statement, it is looking for a
variable named `T' but sees none. To help with generics, try imagining
replacing that `T' with, say, `Shape' everywhere. db.query(Shape) makes
no sense, so db.query(T) makes as little sense.
Since the actual bound type of T is indeterminable at runtime, the only
current workaround is to pass in a Class<? extends T> somehow to the
class and then forward that off.
--
Beware of bugs in the above code; I have only proved it correct, not
tried it. -- Donald E. Knuth
| |
| Jacob Singer 2007-11-15, 7:58 pm |
| You're right db.query(Shape) does not work but db.query(Shape.class)
is good to go because query() is expecting a class type. So what I
need is the generics equivalent of Shape.class and I thought that
would be `T' but it is not so.
On Thu, 15 Nov 2007 22:17:27 GMT, Joshua Cranmer
<Pidgeot18@verizon.invalid> wrote:
>Joshua Cranmer wrote:
> To help with generics, try imagining replacing that `T' with, say,
>`Shape' everywhere. db.query(Shape) makes no sense, so db.query(T)
>makes as little sense.
| |
| Roedy Green 2007-11-15, 7:58 pm |
| On Thu, 15 Nov 2007 21:53:45 GMT, Jacob Singer <long@farside.com>
wrote, quoted or indirectly quoted someone who said :
> ObjectSet<? extends Shape> os = db.query(T); <-- error here
query expects a variable not a generic Type. Under the hood any T
will turn into an Object declaration, clearly not what you wan there.
See http://mindprod.com/jgloss/generics.html
--
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
| |
| Jacob Singer 2007-11-15, 7:58 pm |
| Disregard last posting. I didn't understand what Cranmer said earlier
about passing in a Class<? extends T> but I think I do now. I think I
need to change MyClass' declaration so that when I instaniate it I use
MyClass<Shape, Shape.class> myclass = new MyClass<Shape,
Shape.class>();
The second parameter should be sufficient to make query() happy. The
problem now is my understanding of generics syntax is so poor I don't
know how to declare MyClass with the second type.
public class MyClass<T extends Shape, Class<U> extends T> ????
Any wizards know how to syntax this?
| |
|
| Jacob Singer wrote:
> You're right db.query(Shape) does not work but db.query(Shape.class)
> is good to go because query() is expecting a class type. So what I
> need is the generics equivalent of Shape.class and I thought that
> would be `T' but it is not so.
Please do not top-post.
As Joshua explained, T is not a variable. Shape.class is a variable. T is
like 'Shape' in the expression, as Joshua said, not 'Shape.class'.
You need the equivalent of T.class, except for you can't use that so you have
to pass in a Class<? extends T> somehow, just as Joshua said.
> public class ObjectContainer ...
Ellipses don't compile, and "Object" is not a good class-name part.
Another approach is to make the Container class generic, too.
public class Container<T extends Shape>
{
public static MySet <T> query()
// Notice how the keyword "Class" is spelled with an upper-case 'C'.
{
return null;
}
}
<!-- -->
public class MyUsefulName <T extends Shape>
{
private final Container<T> container = initContainer();
public void exec()
{
MySet <T> stuff = container.query();
doMoreWith( stuff );
}
}
I didn't try to compile this, but it's a start for sure.
--
Lew
| |
|
| Lew wrote:
> // Notice how the keyword "Class" is spelled with an upper-case 'C'.
Oops. This comment is both wrong and out of place.
It was the class 'Class' that's spelled in upper case, as it was in the
original post, correctly, and I had at one point intended to remark how it
differs from the keyword 'class' in having an upper-case first letter. In
clipping the post I left a fragment of that comment in.
--
Lew
| |
| Jacob Singer 2007-11-16, 5:01 am |
| Problem is solved.
I passed Class<? extends T> in via the constructor and query() is
happy. The universe is at peace once again.
thanks to all. I still hate generics syntax.
| |
| Joshua Cranmer 2007-11-16, 7:17 pm |
| Jacob Singer wrote:
> thanks to all. I still hate generics syntax.
Your problem is not so much the syntax (do you like C++'s templates any
better?) but the way they are handled in Java: generics data is, for all
practical purposes, destroyed at compile-time. There are proposals to go
the full yard and actually have generics poke through at runtime
(imagine being able to do, say, new T[]), which would fix most of the
problems, but they seem to be somewhat languishing. Something about
closures seems to be preempting them... ;-)
--
Beware of bugs in the above code; I have only proved it correct, not
tried it. -- Donald E. Knuth
|
|
|
|
|