http://gwt-code-reviews.appspot.com/1455802/diff/5002/user/src/com/google/gwt/dom/builder/shared/ElementBuilderBase.java
File user/src/com/google/gwt/dom/builder/shared/ElementBuilderBase.java
(right):
http://gwt-code-reviews.appspot.com/1455802/diff/5002/user/src/com/google/gwt/dom/builder/shared/ElementBuilderBase.java#newcode106
user/src/com/google/gwt/dom/builder/shared/ElementBuilderBase.java:106:
<B extends ElementBuilderBase<?>> B end(Class<B> clazz);
On 2011/06/24 16:48:18, jlabanca wrote:
On 2011/06/23 22:46:24, skybrian wrote:
> I'm wondering if we need the variations on end() that take a class.
In
> particular, this looks like a mismatch:
>
> out.startDiv().startH1().text("Heading").end(DivBuilder.class)
>
> It also doubles the number of end() method variants.
>
> Though it's ugly, perhaps this would be better:
>
> out.startDiv()
> .startH1().text("Heading").end().backTo(DivBuilder.class)
> .text("Some text")
> .startH2().text("Hello").endH2()
> .end()
>
>
> Although, my personal preference is to not chain so many methods.
Perhaps:
I agree, but I don't want to preclude users from chaining methods if
they want
to.
>
> out.startDiv();
> out.asDiv().startH1().text("hello").endH1();
> out.asDiv().text("hello");
> out.asDiv().startH2().text("hello").endH2();
> out.endDiv();
>
> If you squint at it right, the "asDiv()" call looks sort of like an
indent.
>
> Or perhaps:
>
> DivBuilder __div = out.asDiv();
>
> out.startDiv();
> __div.startH1().text("Heading").end();
> __div.text("Some text");
> __div.startH2().text("Hello").end();
> out.end();
>
> Normally I'd be wary of mixing up local variables, but since they
all point to
> the same instance, it seems fairly harmless.
We could add an ElementBuilderBase.as(Class<?>) method that casts the
instance
to the specific class. The endXXX() methods would return
ElementBuilderBase
(which they currently do if the user doesn't specify a class
parameter). The
user can then cast it to a specific subtype if needed.
There is precedent for as() method. Element.as() will cast one
element type to
another, although Elements are JSOs, so its always safe to switch
between them.
In our case, we rely on Java to throw a ClassCastException.
Alternatively, we can define end() using a parameterized return type:
<E extends ElementBuilderBase<?>> E end();
By default, end() returns an ElementBuilderBase, but users can
parameterize the
return type. The advantage of this approach is that you don't need an
additional call to "as()" or "backTo", and it drops the ".class", so
its
slightly less verbose.
// Returns ElementBuilderBase
out.startDiv().startH1().text("Heading").end();
// Returns DivBuilder.
out.startDiv().startH1().text("Heading").<DivBuilder>end()
Update:
For the end() method, I like the parameterized return type:
<E extends ElementBuilderBase<?>> E end();
If you aren't chaining, you'll never need to parameterize. If you are
chaining, you probably won't need to parameterize because end() returns
an ElementBuilderBase by default, which is enough to start the next
child. If you do need to parameterize, you can just parameterize the
return type with the class name.
http://gwt-code-reviews.appspot.com/1455802/
--
http://groups.google.com/group/Google-Web-Toolkit-Contributors