Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the Dart "Expando" feature about, what does it do?

Tags:

Have been seeing the term "Expando" used recently with Dart. Sounds interesting. The API did not provide much of a clue to me.

An example or two could be most helpful!

(Not sure if this is related, but I am most anxious for a way to add methods (getters) and/or variables to a class. Hoping this might be a key to solving this problem. (hint: I am using the Nosuchmethod method now and want to be able to return the value of the unfound method.))

Thanks in advance,

_swarmii

like image 919
george koller Avatar asked Nov 13 '12 09:11

george koller


1 Answers

Expandos allow you to associate objects to other objects. One very useful example of this is an HTML DOM element, which cannot itself be sub-classed. Let's make a top-level expando to add some functionality to an element - in this case a Function signature given in the typedef statement:

typedef CustomFunction(int foo, String bar);  Expando<CustomFunction> domFunctionExpando = new Expando<CustomFunction>(); 

Now to use it:

main(){    // Assumes dart:html is imported    final myElement = new DivElement();     // Use the expando on our DOM element.    domFunctionExpando[myElement] = someFunc;     // Now that we've "attached" the function to our object,    // we can call it like so:    domFunctionExpando[myElement](42, 'expandos are cool'); }  void someFunc(int foo, String bar){   print('Hello. $foo $bar'); } 
like image 151
John Evans Avatar answered Sep 22 '22 21:09

John Evans