Sunday, November 28, 2010

Re: Simple question about GWT.create() signature

On 26 nov, 21:12, "Alessandro Carraro (JUG Padova)"
<carraro.alessan...@gmail.com> wrote:
> A simple qestion, sorry if it is a FAQ:
>
> Why
>   public static <T> T create(Class<?> classLiteral)
> and not
>   public static <T> T create(Class<T> classLiteral)

With GWT-RPC you pass the "synchronous" (extends RemoteService)
interface as an argument and it returns an implementation of the
"asynchronous" interface.
That wouldn't be possible with the Class<T> argument.

> the second one would save me from a lot of unnecessary casts (IMHO).

I hardly ever needed a cast:
- either the GWT.create() result is assigned to a variable, and javac/
GWT compiler is smart enough to infer the generic type argument
- or if it's not possible, you can use a "generic call" instead of a
cast: GWT.<MyClass>create(...) but I admit it adds the same
boilerplate as a cast...

> What's worse, I tried to write the helper function:
>
>         @SuppressWarnings("unchecked")
>         public static <T> T create(Class<T> classLiteral) {
>                 return (T) GWT.create(classLiteral);
>         }
>
> But after a month I discovered that the compiler does not like it at
> all (it complains that GWT.create must be called only using class
> literals... could be nice if it could find that MY helper is called
> with only using  literals...

It's been requested several types in the past (including by people
that now work at Google on GWT) but I guess it's either low priority
or it would require too much re-work of the compiler's internals.

> Is it possible to get a hook into the compiler phase to replace all
> invocations of my helper 'inlining' it?

I don't think so; but is it really worth it? risking bugs in your
"hook" just to save you from typing a few characters (that, moreover,
any good IDE is capable of generating for you)

--
You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group.
To post to this group, send email to google-web-toolkit@googlegroups.com.
To unsubscribe from this group, send email to google-web-toolkit+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-web-toolkit?hl=en.

No comments:

Post a Comment