IssueRaiser

Static Metaprogramming

**Metaprogramming** refers to code that operates on other code as if it were data. It can take code in as parameters, reflect over it, inspect it, create it, modify it, and return it. ***Static* metaprogramming** means doing that work at compile-time, and typically modifying or adding to the program based on that work. Today it is possible to do static metaprogramming completely outside of the language - using packages such as [build_runner][build_runner] to generate code and using the analyzer apis for introspection. These separate tools however are not well integrated into the compilers or tools, and it adds a lot of complexity where this is done. It also tends to be slower than an integrated solution because it can't share any work with the compiler. ## Sample Use Case - Data Classes The [most requested open language issue][data_classes_issue] is to add data classes. A data class is essentially a regular Dart class that comes with an automatically provided constructor and implementations of `==`, `hashCode`, and `copyWith()` (called `copy()` [in Kotlin][kotlin_copy]) methods based on the fields the user declares in the class. The reason this is a *language* feature request is because there’s no way for a Dart library or framework to add data classes as a reusable mechanism. Again, this is because there isn’t any easily available abstraction that lets a Dart user express “given this set of fields, add these methods to the class”. The `copyWith()` method is particularly

Posted by jakemac53 on Mar 1, 2021, 4:08 PM

Tags

Related issues

Raise real-world problems, find people facing the same issue, and discuss solutions.3,000 issues0 comments

github (Software)

Static Metaprogramming

jakemac53Mar 1, 2021, 4:08 PM

**Metaprogramming** refers to code that operates on other code as if it were data. It can take code in as parameters, reflect over it, inspect it, create it, modify it, and return it. ***Static* metaprogramming** means doing that work at compile-time, and typically modifying or adding to the program based on that work. Today it is possible to do static metaprogramming completely outside of the language - using packages such as [build_runner][build_runner] to generate code and using the analyzer apis for introspection. These separate tools however are not well integrated into the compilers or tools, and it adds a lot of complexity where this is done. It also tends to be slower than an integrated solution because it can't share any work with the compiler. ## Sample Use Case - Data Classes The [most requested open language issue][data_classes_issue] is to add data classes. A data class is essentially a regular Dart class that comes with an automatically provided constructor and implementations of `==`, `hashCode`, and `copyWith()` (called `copy()` [in Kotlin][kotlin_copy]) methods based on the fields the user declares in the class. The reason this is a *language* feature request is because there’s no way for a Dart library or framework to add data classes as a reusable mechanism. Again, this is because there isn’t any easily available abstraction that lets a Dart user express “given this set of fields, add these methods to the class”. The `copyWith()` method is particularly

Comments (0)

No comments yet. Start the discussion.

Comment on this issue