The recent announcement of Dart Macros at Google I/O 2024 marks a significant milestone in the evolution of the Dart language, ushering in a new era of metaprogramming that promises to enhance the developer experience profoundly. Dart Macros, introduced as a preview in Dart 3.4 with the JsonCodable macro, offers a groundbreaking approach to code generation that integrates directly into the language. This feature allows developers to automate repetitive tasks, like JSON serialization and deserialization, which have long been a pain point in Dart due to the lack of runtime reflection. Introducing macros simplifies these tasks and maintains Dart’s emphasis on performance and developer productivity.
One of the most exciting aspects of Dart macros is how they can raise the level of abstraction in Dart programming. Macros enable developers to create powerful, reusable solutions tailored to their specific needs by allowing code to generate more code at compile time. The preview of the JsonCodable macro demonstrates the potential of this system, offering an elegant solution to JSON handling that seamlessly integrates with existing workflows, including hot reload. This integration ensures that using macros does not disrupt the developer’s workflow, but rather enhances it, making development more efficient and less error-prone.
What’s the state-of-the-art today, and what does this mean for the future of Dart?
First, this feature is still experimental so even though we’ve seen tremendous improvements we would have to wait until we can use it in a production environment. This doesn’t mean we can’t start getting familiarized with the macros system.
How will this impact code generation packages?
As Flutter developers, we rely so much on code generation packages using build_runner. For instance, json_serializable or freezed. This approach, although it works, is not the best, as it takes so many seconds of our lives to re-generate the files with any changes. However, the idea of code generation isn’t wrong. At the end of the day, while using this approach you get confidence that the actual code you are maintaining is smaller, less error prune on details and overall less time writing repetitive code. Well, while these packages are not going to disappear, they are probably going to adopt macros so that the developer experience will increase. The way we will interact with the package will be slightly different, but the essence will be maintained: we will still write annotations and some code will be created for us.
How can we expect these packages to get updated? Is there an actual migration guide?
Regarding a migration guide, because of the type of change it represents it is unlikely we will count on migration tooling, so I think starting using it from scratch in a project would be the easiest path but in bigger ongoing projects the migration (if desired) is going to be quite manual.
How’s the community reacting to Dart Macros?
Overall the reaction has been quite positive since this is a feature that the community has asked for and has been interested in since a long time ago, as we can here.
So far, we’ve seen some packages that are trying to include this approach, like the freezed rewrite dataclass or a simply data_class package that provides things like `copyWith` or `toString`. I think that any package author who has created a package using code generation
should start looking forward to macros to update it.
Conclusions and next steps
In conclusion, introducing Dart macros represents a bold step forward for the Dart language, offering a powerful new tool for developers to enhance their productivity and creativity. As the macro system matures and becomes more widely adopted, we can expect to see a new wave of innovation in the Dart community, with developers leveraging macros to create more efficient, maintainable, and expressive code. This evolution will benefit current Dart users and attract new developers to the language, further solidifying Dart’s position as a leading platform for modern development.
Exciting Announcement
I'm thrilled to announce that Somnio will be participating in Fluttercon USA! I'll also be speaking at the conference, delivering two talks, including "The Power of Macros in Dart." I hope to see you there!
To learn more about my talks and Fluttercon USA, please feel free to contact me.