On Mon, Jan 24, 2011 at 2:17 PM, Greg Brown <[email protected]> wrote:

> Parameterizing the getter is definitely a valid option - possibly better
> than returning List<Object>, since it may help catch more errors at compile
> time.
>

I'm a bit skeptical whether this will increase compiletime error detection
or not.

Since as example shows, they do not detect potential problem.
Also adding an integer will result in runtime error, but it is not detected
by compiler.

main advantage is the ability to assign to List<J> variable.
if we use List<?> as the return type, this can be done with cast, but if we
use List<Object> as return type, cast cannot be applied also.





>
> G
>
>
> On Jan 24, 2011, at 5:07 PM, calathus wrote:
>
> In order to avoid this compiler error issue, we may choose an approach to
> use generic type for method level.
> see the following code. Although from type safety perspective, there are
> quite strange things involved here..
> Originally I considered to use super(e.g, <? super I>) for anonymous type,
> but looks like Java does support this syntax in quite restricted place.
>
>
> ----------------
>
> public class Main {
>
>     static class A {
>         List<?> ts;
>         public List<?> getList() {
>             return ts;
>         }
>         public void setList(List<?> ts) {
>             this.ts = ts;
>         }
>     }
>     interface I {
>         String getName();
>     }
>     static class B {
>         List<? extends I> ts;
>         public <T> List<T> getList() {
>             return (List<T>)ts;
>         }
>         public void setList(List<? extends I> ts) {
>             this.ts = ts;
>         }
>     }
>
>     class J implements I {
>         String name;
>         public String getName() {
>             return name;
>         }
>         public void setName(String name) {
>             this.name = name;
>         }
>     }
>
>     public static void main(String[] args) {
>         {
>             A a = new A();
>             a.setList(new ArrayList<Integer>());
>             List<Object> list = (List<Object>)a.getList();
>             //List<Object> list = a.getList(); // error
>         }
>         {
>             B b = new B();
>             b.setList(new ArrayList<J>());
>             List<I> list1 = b.getList();
>             List<J> list2 = b.getList();
>             List<Object> list3 = b.getList(); // OK
>             b.getList().add(new Integer(4));
>         }
>     }
>
> }
>
>
> On Mon, Jan 24, 2011 at 12:45 PM, Greg Brown <[email protected]> wrote:
>
>> > Another thought, ListView should not implement List interface? basically
>> > a ListView/TableView are lists with an extra graphics support.
>>
>> No, ListView and TableView are views on the model data provided by a List.
>>
>> G
>>
>>
>>
>
>
> --
> Cheers,
> calathus
>
>
>
>
>


-- 
Cheers,
calathus

Reply via email to