On Mon, Feb 23, 2009 at 8:54 AM, Thomas Broyer <[email protected]> wrote: > On 23 fév, 16:36, Ian Petersen <[email protected]> wrote: >> GWT.create() is obviously not what you think it is. > > GWT.create is probably not what you think it is. > > GWT.create(MyClass.class) is *identical* to new MyClass(), except that > you can define rules so that it becomes new MySubClass() at compile- > time depending on the permutation being processed. > > You can see MyClass o = GWT.create(MyClass.class) as the *exact* > equivalent of the following, where the condition is not expressible in > code (GWT doesn't provide a getCurrentUserAgent() for instance), has a > fixed value for a given permutation and is therefore optimized by the > compiler: > MyClass o; > if (<some condition is true>) { > o = new MySubClass(); > } else { > o = new MyClass(); > } > > What's important here is that the "else" clause is *always* there, so > if there isn't any deferred binding rule, MyClass o = GWT.create > (MyClass.class) is *identical* to MyClass o = new MyClass(). > It means that if MyClass is not a concrete class (abstract class or > interface) and/or has no zero-arg constructor, the compiler will fail, > as it would have if you had written new MyClass(). > >> As far as I know, you can't create any widgets with GWT.create(). > > Any widget (any class) with a zero-arg constructor can be instantiated > with GWT.create(), though I wouldn't recommend doing it (see my advice > to Eugen).
Why would you use GWT.create(Foo.class) in the instances that it's equivalent to new Foo()? GWT.create() is really only useful in two cases: you want to choose a class' implementation based on some runtime constant (user agent, locale, etc.) at compile time or you want to generate an implementation based on compile time knowledge (RPC implementations, data binding, image clustering etc.). In any situation that is equivalent to invoking a no-args constructor of the type specified as a parameter to GWT.create(), using GWT.create() instead of just invoking the constructor does nothing more than obfuscate your code. I'll admit I didn't realize you could make use of this obfuscation technique, but, having written a fair number of lines of code that are invoked by GWT.create(), I think it's a special case that can be safely ignored most of the time. I'd be curious for a GWT dev to explain why GWT.create(Foo.class) even degenerates to new Foo() in the absence of a rule for that case. It seems to me that should be a compile-time error. Ian --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/Google-Web-Toolkit?hl=en -~----------~----~----~----~------~----~------~--~---
