Skip to content

Commit cd82e7a

Browse files
committed
clean up macro examples and README.md, remove run.dart script
1 parent e433c0c commit cd82e7a

File tree

7 files changed

+83
-282
lines changed

7 files changed

+83
-282
lines changed

working/macros/example/README.md

+9-13
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,20 @@
11
**DISCLAIMER**: All code in this package is experimental and should be treated
2-
as such.
3-
4-
This package some example macros (under `lib`), as well as some utilities to try
5-
actually running those examples.
2+
as such. The examples are unstable and may or may not work at any given time,
3+
depending on the implementation status.
64

75
## Setup
86

97
Your SDK will need to match roughly the commit pinned in the pubspec.yaml file
108
of this package (see the `ref` lines). Otherwise you will get a kernel version
119
mismatch.
1210

11+
## Examples
12+
13+
The example macros live under `lib/` and there are programs using them under
14+
`bin/`. To try and run an example, you need to enable the `macros` experiment,
15+
`dart --enable-experiment=macros <script>`, but the implementations do not yet
16+
support all the examples here so you should expect errors.
17+
1318
## Benchmarks
1419

1520
There is a basic benchmark at `benchmark/simple.dart`. You can run this tool
@@ -19,12 +24,3 @@ environment (compiler).
1924

2025
This benchmark uses a synthetic program, and only benchmarks the overhead of
2126
running the macro itself, and the communication to and from the host program.
22-
23-
## Examples
24-
25-
There is an example program at `bin/user_main.dart`. This _cannot_ be directly
26-
executed but you can compile and execute it with the `bin/run.dart` script.
27-
28-
**NOTE**: This is not meant to be a representative example of how a script using
29-
macros would be compiled and ran in the real world, but it does allow you to
30-
execute the program.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import 'package:macro_proposal/auto_dispose.dart';
2+
3+
void main() {
4+
var state = MyState(a: ADisposable(), b: BDisposable(), c: 'hello world');
5+
state.dispose();
6+
}
7+
8+
@AutoDispose()
9+
class MyState extends State {
10+
final ADisposable a;
11+
final ADisposable? a2;
12+
final BDisposable b;
13+
final String c;
14+
15+
MyState({required this.a, this.a2, required this.b, required this.c});
16+
17+
@override
18+
String toString() => 'MyState!';
19+
}
20+
21+
class State {
22+
void dispose() {
23+
print('disposing of $this');
24+
}
25+
}
26+
27+
class ADisposable implements Disposable {
28+
void dispose() {
29+
print('disposing of ADisposable');
30+
}
31+
}
32+
33+
class BDisposable implements Disposable {
34+
void dispose() {
35+
print('disposing of BDisposable');
36+
}
37+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Copyright (c) 2022, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
import 'package:macro_proposal/data_class.dart';
6+
7+
void main() {
8+
var joe = User(
9+
age: 25,
10+
name: 'Joe',
11+
username: 'joe1234');
12+
print(joe);
13+
14+
var phoenix = joe.copyWith(name: 'Phoenix', age: 23);
15+
print(phoenix);
16+
}
17+
18+
@DataClass()
19+
class User {
20+
final int age;
21+
final String name;
22+
final String username;
23+
}
24+
25+
@DataClass()
26+
class Manager extends User {
27+
final List<User> reports;
28+
}

working/macros/example/bin/run.dart

-152
This file was deleted.

working/macros/example/bin/user_main.dart

-112
This file was deleted.

working/macros/example/lib/auto_dispose.dart

+5-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ macro class AutoDispose implements ClassDeclarationsMacro, ClassDefinitionMacro
2323
}
2424

2525
builder.declareInType(DeclarationCode.fromParts([
26+
// TODO: Remove external once the CFE supports it.
2627
'external void dispose();',
2728
]));
2829
}
@@ -62,7 +63,10 @@ macro class AutoDispose implements ClassDeclarationsMacro, ClassDefinitionMacro
6263
var disposeBuilder = await builder.buildMethod(disposeMethod.identifier);
6364
disposeBuilder.augment(FunctionBodyCode.fromParts([
6465
'{\n',
65-
if (!disposeMethod.hasBody) 'super.dispose();' else 'augmented();',
66+
if (disposeMethod.hasExternal || !disposeMethod.hasBody)
67+
'super.dispose();'
68+
else
69+
'augmented();',
6670
...disposeCalls,
6771
'}',
6872
]));

0 commit comments

Comments
 (0)