Skip to content

Commit

Permalink
clean up macro examples and README.md, remove run.dart script (#3582)
Browse files Browse the repository at this point in the history
- Delete run.dart script and update the README.md to indicate you can just pass `--enable-experiment=macros`.
- Break up `user_main.dart` into separate scripts for each macro example, which allows you to run the ones that do work, and also keeps the example apps more targetted. These were only combined into one previously to make it easier to run them through `run.dart`.
- Update the observable example so it is runnable.
- Update the data class example to create the unnamed constructor instead of one called `gen`.
  • Loading branch information
jakemac53 authored Jan 23, 2024
1 parent fa5a895 commit b1e9584
Show file tree
Hide file tree
Showing 11 changed files with 100 additions and 287 deletions.
22 changes: 9 additions & 13 deletions working/macros/example/README.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
**DISCLAIMER**: All code in this package is experimental and should be treated
as such.

This package some example macros (under `lib`), as well as some utilities to try
actually running those examples.
as such. The examples are unstable and may or may not work at any given time,
depending on the implementation status.

## Setup

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

## Examples

The example macros live under `lib/` and there are programs using them under
`bin/`. To try and run an example, you need to enable the `macros` experiment,
`dart --enable-experiment=macros <script>`, but the implementations do not yet
support all the examples here so you should expect errors.

## Benchmarks

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

This benchmark uses a synthetic program, and only benchmarks the overhead of
running the macro itself, and the communication to and from the host program.

## Examples

There is an example program at `bin/user_main.dart`. This _cannot_ be directly
executed but you can compile and execute it with the `bin/run.dart` script.

**NOTE**: This is not meant to be a representative example of how a script using
macros would be compiled and ran in the real world, but it does allow you to
execute the program.
1 change: 0 additions & 1 deletion working/macros/example/analysis_options.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,5 @@ analyzer:
# TODO: remove these when the analyzer supports macros enough to run them
- bin/checks_main.dart
- bin/injectable_main.dart
- bin/user_main.dart
- bin/json_serializable_main.dart
- bin/observable_main.dart
41 changes: 41 additions & 0 deletions working/macros/example/bin/auto_dispose_main.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

import 'package:macro_proposal/auto_dispose.dart';

void main() {
var state = MyState(a: ADisposable(), b: BDisposable(), c: 'hello world');
state.dispose();
}

@AutoDispose()
class MyState extends State {
final ADisposable a;
final ADisposable? a2;
final BDisposable b;
final String c;

MyState({required this.a, this.a2, required this.b, required this.c});

@override
String toString() => 'MyState!';
}

class State {
void dispose() {
print('disposing of $this');
}
}

class ADisposable implements Disposable {
void dispose() {
print('disposing of ADisposable');
}
}

class BDisposable implements Disposable {
void dispose() {
print('disposing of BDisposable');
}
}
4 changes: 4 additions & 0 deletions working/macros/example/bin/checks_main.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

@ChecksExtensions([Person])
library;

Expand Down
25 changes: 25 additions & 0 deletions working/macros/example/bin/data_class_main.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

import 'package:macro_proposal/data_class.dart';

void main() {
var joe = User(age: 25, name: 'Joe', username: 'joe1234');
print(joe);

var phoenix = joe.copyWith(name: 'Phoenix', age: 23);
print(phoenix);
}

@DataClass()
class User {
final int age;
final String name;
final String username;
}

@DataClass()
class Manager extends User {
final List<User> reports;
}
4 changes: 4 additions & 0 deletions working/macros/example/bin/injectable_main.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

import 'package:macro_proposal/injectable.dart';

void main() {
Expand Down
8 changes: 6 additions & 2 deletions working/macros/example/bin/observable_main.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

import 'package:macro_proposal/observable.dart';

void main() {
Expand All @@ -16,6 +20,6 @@ class ObservableUser {
ObservableUser({
required int age,
required String name,
}) : _age = age,
_name = name;
}) : _age = age,
_name = name;
}
152 changes: 0 additions & 152 deletions working/macros/example/bin/run.dart

This file was deleted.

Loading

0 comments on commit b1e9584

Please sign in to comment.