dartdart-mirrors

Dart meta programming features


Will there be an equivelent of the c# Reflection.Emit namespace in dart?

Reflection.Emit has a number of classes that are used to build types at run time and adding properties, configering their getters and setter and building methods and event handlers all at run time, which is really powerfull when it comes to metaprogramming.

my idea is about generating my data models at run time and caching them in a map so i can create instances at run time and add new methods and properties to them when i need to and not having to use mirrors often after generating the class, this could be really useful when writing ORMs and more dynamic applications where you use reflection once rather than using it every time you need to modify an instance

My questions are:


Solution

  • I have seen discussions that this should be supported at some time but as far as I know will not be started to work on in the near future.

    Similar requirements are usually solved by code generation at build time (Polymer, Angular, others) by transformers which analyze the code and generated code for reflective property access or code snippets in HTML.

    Smoke is a package that aims to simplify this.

    Code generation has the advantage that the amount of code needed to be downloaded by the client is much smaller. When you do code generation at runtime you need a compiler and that is a lot of code that needs to be downloaded into the browser.

    try.dartlang.org takes a such an approach. The source is available here https://code.google.com/p/dart/source/browse/branches/bleeding_edge/dart/site/try/ . It includes dart2js (built to JavaScript) and runs a background isolate that compiles the Dart code to JS.