-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Note that it's possible to do this:

public <T> List<T> getList() {
  return (List<T>)ts;
}

without the ghastly Class<T> param, and make use of one of the few cases
where Java can infer type params:

List<Foo> myList = listView.getList()

I write type-inferring factories all the time like this:

static <T> ArrayList<T> makeArrayList() {
  return new ArrayList<T>();
}

List<Foo<Bar<List<Baz>>>> = makeArrayList();

To only specify types once.

Of course, this has the same problem of errors only showing up at
runtime, and in a depressingly high number of cases, you still need to
specify the <T> using Java's weird syntax - listView.<Foo>getList().

calathus wrote:
> 
> On Tue, Jan 25, 2011 at 11:42 AM, Greg Brown <[email protected] 
> <mailto:[email protected]>> wrote:
> 
>      > Since generic version may give wrong impression that it is type safe, 
> it
>     may be better to provide two version for getter, one is just return
>     List<object>, another will return List<T> taking class value as the
>     argument. also we may use generic method for setter. see following sample 
> code.
>     ...
>      >         public List<Object> getList() {
>      >             return (List<Object>)ts;
>      >         }
>      >         public <T> List<T> getList(Class<T> cls) {
>      >             return (List<T>)ts;
>      >         }
> 
>     Yuck. I think it is probably best to leave it as-is.
> 
> 
> I think this approach will eliminate using cast for adding element(which is 
> very 
> frequently used), while current one still requires cast.
> And I think most of case, the second case won't be required.
> 
>  
> 
> 
> 
> -- 
> Cheers,
> calathus
> 
> 
> 

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iEYEARECAAYFAk0/MpYACgkQ5IyIbnMUeTvFMACdF6xKJJlVNs6XdP3V5A4so7It
5C0An1f+9aoWszsC5MPrNER97BRs41Rr
=tAk7
-----END PGP SIGNATURE-----

Reply via email to