What's a good use of an Expando?

1,124 views
Skip to first unread message

mythz

unread,
Apr 16, 2013, 2:34:38 PM4/16/13
to mi...@dartlang.org
The API docs for it are pretty bare with the core API revolved around overloading the indexer '[]' operator:

I can't find any examples of when it's a good idea to use an Expando even in the core libraries?

What is Expando for? when should we use it? and what benefits does it have over a simple Map?

John Messerly

unread,
Apr 16, 2013, 2:43:24 PM4/16/13
to General Dart Discussion
It lets you associate data with any object. This can be pretty convenient.

On the VM, it has weak reference behavior. In JS, it stores data on the object itself, in a hidden way.

Unlike associating data via a Map, using an Expando won't keep the key object alive. So it's essentially a weak map.

As to why it isn't used much: I can't speak for everyone, but I avoid it because of:




--
Consider asking HOWTO questions at Stack Overflow: http://stackoverflow.com/tags/dart
 
 

Alex Tatumizer

unread,
Apr 16, 2013, 3:09:02 PM4/16/13
to mi...@dartlang.org
Is it a weak identity hashmap then?
Named "expando" to sound more romantic?
Where O(N^2) comes from in a hashmap?
I'm confused :(
I was wondering about expando, but was afraid to ask - thought it was something just sitting there until someone notices it and removes. No?



Joseph Moniz

unread,
Apr 16, 2013, 3:31:42 PM4/16/13
to mi...@dartlang.org
It's use still seems some what confusing. Is it a class you extend, kind of like a mixin for [] and []=, or is it used in paramaterized type manner as it's constructor seems to suggest that somehow forwards method invocations to an instance of it's paramaterized type?

- Joseph Moniz

"Wake up early, Stay up late, Change the world"


John Messerly

unread,
Apr 16, 2013, 3:44:58 PM4/16/13
to General Dart Discussion
On Tue, Apr 16, 2013 at 12:09 PM, Alex Tatumizer <tatu...@gmail.com> wrote:
Is it a weak identity hashmap then?

Yup.
 
Where O(N^2) comes from in a hashmap?

Yeah, it's a bug :)
 
I was wondering about expando, but was afraid to ask - thought it was something just sitting there until someone notices it and removes. No?

Weak maps are a useful feature. They're being added to ES6 too: http://wiki.ecmascript.org/doku.php?id=harmony:weak_maps

John Messerly

unread,
Apr 16, 2013, 3:49:44 PM4/16/13
to General Dart Discussion
On Tue, Apr 16, 2013 at 12:31 PM, Joseph Moniz <joseph...@gmail.com> wrote:
It's use still seems some what confusing. Is it a class you extend, kind of like a mixin for [] and []=, or is it used in paramaterized type manner as it's constructor seems to suggest that somehow forwards method invocations to an instance of it's paramaterized type?

Think of it as a map, where you use objects as keys. Use like:

final myProp = new Expando<int>('optional human friendly name for debugging');

someFunction(Node node) {
  // Associate data with some arbitrary object.
  // Maybe it came from a library I don't control, like dart:html, so I can't just add a field there  
  myProp[node] = 42;
}

someOtherFunction(node) {
  // Get the data we associated earlier.
  int theAnswer = myProp[node];
  print(theAnswer);
}

See also:

Alex Tatumizer

unread,
Apr 16, 2013, 3:54:43 PM4/16/13
to mi...@dartlang.org
> Weak maps are a useful feature
Absolutely. I use them very often in java. I just couldn't recognize them in expandos. Not documented well.
Good to know, thanks!



Sean Eagan

unread,
Apr 16, 2013, 7:45:32 PM4/16/13
to General Dart Discussion
ES6's WeakMap doesn't expose iteration functionality so as to keep the GC semantics unobservable.  I assume Expando doesn't implement Map (or include Map in it's name) for the same reason.

Cheers,
Sean Eagan


Reply all
Reply to author
Forward
0 new messages