Upgrade to Pro — share decks privately, control downloads, hide ads and more …

A quick overview of static metaprogramming wi...

A quick overview of static metaprogramming with Dart macros

A quick overview of Dart Macros, a feature introduced in Dart 3.5 that empowers developers to generate code and reduce the need for boilerplate code. In 10 minutes, we'll understand what they are, the motivation behind this new feature, and how they can be applied with code examples.

Victoria Alajemba

October 24, 2024
Tweet

Other Decks in Programming

Transcript

  1. Agenda • What is static metaprogramming? • What are Dart

    macros? • Motivation behind this feature • What can macros do? • Applications • Alternatives to Dart macros • Code
  2. What is static metaprogramming? Static metaprogramming is a technique of

    writing code that will operate on other code at compile time. This “metaprogram” will take the code as its input, inspect it, and augment it.
  3. What are Dart macros? Dart macros is a Dart solution

    for static metaprogramming. It relies on Dart’s augmentation feature to create or modify code. Benefits include building in real-time, integrated directly into Dart, lives in memory, supports easy inspection and reduces boilerplate code
  4. Motivation behind this feature • Boilerplate code • Developer requests

    • Diverse methodologies • Language flexibility
  5. What can macros do? • Inspect classes and their properties,

    methods, constructors and their parameters • Augment existing classes by adding new methods or fields • Create new classes • Serve as ingredients in other macros
  6. Applications • JSON serialization and deserialization, • Data classes •

    Manipulating flutter widgets • Parsing command line arguments
  7. Alternatives to Dart macros • mirrors library • build_runner •

    Code generation packages like Mason bricks • IDE features like code snippets and plugins
  8. Useful Links & References • Motivation behind Dart macros •

    language/working/macros/feature-s pecification.md at main • flutter.dev/go/stateful-macro • Macros (experimental) | Dart • Macros samples • Json macro • build_runner | Dart • The fear of dart:mirrors