Google Groups

Re: [misc] Sharing my experiences


Sam McCall May 2, 2012 5:02 AM
Posted in group: Dart Misc
On Mon, Apr 30, 2012 at 8:11 PM, Bob Nystrom <rnys...@google.com> wrote:
>> -new Element.html("<div></div>") or new Element.tag("div") is weakly
>> typed, fell a few times in this trap.
>
> I'm not sure what you mean by "weakly typed" here. It is true that those
> constructors may return a different concrete type than what appears after
> the "new".
I think 'dynamically typed' is more accurate than 'weakly typed' here:
Element.tag returns an Element, which is appropriate when you don't
know the type at compile-time.
The problem is that when you *do* know that you want a <button>, the
only way to get one is new Element.tag('button') and know from the
docs that it will return a ButtonElement.
You won't get any assistance from the type-checker or editor, because
they can't read the docs.

> In all cases, though, they should be returning a subtype, so that
> shouldn't be a trap for you to fall into.
It'd be equally correct if all API methods were declared to return
Object. I spent enough time with Ruby to know it can be a trap :-)

> What problems did you run into here?
No completion in the editor (for chained expressions, cascades, or
Dynamic-typed locals with type inference): e.g.
  doc.body.add(new Element.tag('a')..href='/foo'..text='Foo')

No warnings when you assign to the wrong subtype, likely when
modifying a statement:
  InputElement addButton() {
    return new Element.tag('input')..type='submit'; // old
    return new Element.tag('button'); // new
  }