Skip to content

Commit fec8409

Browse files
authored
add macro keyword (#2008)
1 parent bb1664d commit fec8409

File tree

3 files changed

+29
-12
lines changed

3 files changed

+29
-12
lines changed

analysis_options.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
analyzer:
22
exclude:
33
- accepted/2.3/spread-collections/examples/**
4+
# Uses not-yet-existing language features
5+
- working/macros/example/**

working/macros/example/data_class.dart

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import '../api/code.dart';
55

66
const dataClass = _DataClass();
77

8-
class _DataClass implements ClassDeclarationsMacro, ClassDefinitionMacro {
8+
macro class _DataClass implements ClassDeclarationsMacro, ClassDefinitionMacro {
99
const _DataClass();
1010

1111
@override
@@ -29,7 +29,7 @@ class _DataClass implements ClassDeclarationsMacro, ClassDefinitionMacro {
2929

3030
const autoConstructor = _AutoConstructor();
3131

32-
class _AutoConstructor implements ClassDeclarationsMacro {
32+
macro class _AutoConstructor implements ClassDeclarationsMacro {
3333
const _AutoConstructor();
3434

3535
@override
@@ -108,7 +108,7 @@ class _AutoConstructor implements ClassDeclarationsMacro {
108108
const copyWith = _CopyWith();
109109

110110
// TODO: How to deal with overriding nullable fields to `null`?
111-
class _CopyWith implements ClassDeclarationsMacro {
111+
macro class _CopyWith implements ClassDeclarationsMacro {
112112
const _CopyWith();
113113

114114
@override
@@ -146,7 +146,7 @@ class _CopyWith implements ClassDeclarationsMacro {
146146

147147
const hashCode = _HashCode();
148148

149-
class _HashCode implements ClassDeclarationsMacro, ClassDefinitionMacro {
149+
macro class _HashCode implements ClassDeclarationsMacro, ClassDefinitionMacro {
150150
const _HashCode();
151151

152152
@override
@@ -175,7 +175,7 @@ external int get hashCode;'''));
175175

176176
const equality = _Equality();
177177

178-
class _Equality implements ClassDeclarationsMacro, ClassDefinitionMacro {
178+
macro class _Equality implements ClassDeclarationsMacro, ClassDefinitionMacro {
179179
const _Equality();
180180

181181
@override
@@ -204,7 +204,7 @@ external bool operator==(Object other);'''));
204204

205205
const toString = _ToString();
206206

207-
class _ToString implements ClassDeclarationsMacro, ClassDefinitionMacro {
207+
macro class _ToString implements ClassDeclarationsMacro, ClassDefinitionMacro {
208208
const _ToString();
209209

210210
@override

working/macros/feature-specification.md

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -293,17 +293,32 @@ introspection APIs. These macros can fully introspect on any type reachable from
293293
the declarations they are applied to, including introspecting on members of
294294
classes, etc.
295295

296-
## Macro definitions
296+
## Macro declarations
297297

298-
Macro definitions do not have any unique syntax or language features. They are
299-
regular Dart class declarations. They are macros by virtue of the fact that they
300-
implement one or more special "macro" interfaces defined by the Dart core
301-
libraries.
298+
Macros are a special type of class, which are preceded by the `macro` keyword,
299+
and have some additional limitations that classes don't have.
302300

303-
*Note: The API is still being designed, and lives [here][api].*
301+
This keyword allows compilers (and users) to identify macros at a glance,
302+
without having to check the type hierarchy to see if they implement `Macro`.
303+
304+
See some example macros [here][examples].
305+
306+
[examples]: https://github.com/dart-lang/language/tree/master/working/macros/example
307+
308+
### Macro limitations/requirements
309+
310+
- All macro constructors must be marked as const.
311+
- See the [Macro Arguments](#Macro-arguments) section to understand how macro
312+
constructors are invoked, and their limitations.
313+
- All macros must implement at least one of the `Macro` interfaces.
314+
- Macros cannot be abstract.
315+
316+
*Note: The Macro API is still being designed, and lives [here][api].*
304317

305318
[api]: https://github.com/dart-lang/language/blob/master/working/macros/api
306319

320+
### Writing a Macro
321+
307322
Every macro interface is a subtype of a root [Macro][] [marker interface][].
308323
There are interfaces for each kind of declaration macros can be applied to:
309324
class, function, etc. Then, for each of those, there is an interface for each

0 commit comments

Comments
 (0)