Slide 1

Slide 1 text

Static Metaprogramming with Dart macros A quick overview of Victoria Chuks Alajemba

Slide 2

Slide 2 text

Agenda ● What is static metaprogramming? ● What are Dart macros? ● Motivation behind this feature ● What can macros do? ● Applications ● Alternatives to Dart macros ● Code

Slide 3

Slide 3 text

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.

Slide 4

Slide 4 text

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

Slide 5

Slide 5 text

Motivation behind this feature ● Boilerplate code ● Developer requests ● Diverse methodologies ● Language flexibility

Slide 6

Slide 6 text

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

Slide 7

Slide 7 text

Applications ● JSON serialization and deserialization, ● Data classes ● Manipulating flutter widgets ● Parsing command line arguments

Slide 8

Slide 8 text

Alternatives to Dart macros ● mirrors library ● build_runner ● Code generation packages like Mason bricks ● IDE features like code snippets and plugins

Slide 9

Slide 9 text

Code

Slide 10

Slide 10 text

without macro with macro JSON

Slide 11

Slide 11 text

without macro with macro Data class

Slide 12

Slide 12 text

Before writing your macro

Slide 13

Slide 13 text

@JsonCodable()

Slide 14

Slide 14 text

@JsonCodable() json ^0.20.2

Slide 15

Slide 15 text

generate code targets classes conditions @Singleton() singleton_macro ^0.0.2-dev.1

Slide 16

Slide 16 text

linkedin.com/in/alajemba Thank you

Slide 17

Slide 17 text

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