Re: 1.5 generic question
From: Daniel Bonniot (Daniel.Bonniot_at_inria.fr)
Date: 12/17/03
- Next message: Electric Natcho: "ejb cleanup"
- Previous message: Brad BARCLAY: "Re: jusb"
- In reply to: Chris: "1.5 generic question"
- Next in thread: Thomas A. Li: "Re: 1.5 generic question"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Wed, 17 Dec 2003 17:51:14 +0100
> then using generics I could define this:
>
> private List<MutableAddress> list;
>
> but could I 'down-cast' this list to do the following:
>
> List<Address> getList() { return list; }
No you can't. Otherwise, your client would be able to add an Address
which is not a MutableAddress in the list, which would make the type of
your private list field a fallacy.
So if you want such behaviour, you need to guarantee that the client
will not modify the original list. There are (at least) two ways to do
that: return a copy of the list, or return an unmodifiable version of
the list:
List<Point> get() = new ArrayList(l);
List<Point> get() = Collections.unmodifiableList(l);
These method definitions are in the syntax of Nice (the syntax = ...; is
a short-hand for { return ...; }). If expect the same idea will work in
Java 1.5, if they gave the right types to these operations, which was
not always the case as far as I could experience... Does anybody with
access to the prototype can test it?
Daniel
The Nice extension of Java: http://nice.sf.net
- Next message: Electric Natcho: "ejb cleanup"
- Previous message: Brad BARCLAY: "Re: jusb"
- In reply to: Chris: "1.5 generic question"
- Next in thread: Thomas A. Li: "Re: 1.5 generic question"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|
|