-
Notifications
You must be signed in to change notification settings - Fork 7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Building expressions from statements #18
Comments
That is one of my motivations behind the issue #13 I was originally thinking that static metaprogramming would allow us to implement new language features by generating patch code. Like how babel in JS allows us to use async/await but target a version of JS that does not support it yet For example one thing I had in mind was converting Future<T> future
Future<T> future2
Widget build(context) {
print('a');
AsyncSnapshot<T> snapshot = $useFuture(future);
AsyncSnapshot<T> snapshot2 = $useFuture(future2);
return Text('$snapshot $snapshot2');
} into: Widget build(context) {
print('a');
return FutureBuilder<T>(
future: future,
builder: (context, snapshot) {
FutureBuilder<T>(
future: future2,
builder: (context, snapshot2) {
return Text('$snapshot $snapshot2');
},
),
},
);
} |
We have considered a form of macros that aren't ran on declarations, but instead on statements/expressions/elements. And they would likely be able to transform those things arbitrarily. Likely these would run in a new, 4th phase. But we haven't spent much time yet investigating it. |
@Hixie would you be able to elaborate a bit more on exactly the type of api you would like to be able to expose here? How would you envision the user applying the macro, and how would it transform the code? |
I have no idea. I tried writing sample versions of ways to do this a half dozen times and couldn't come up with anything compelling. |
Ok, ya I think I understand what you want to achieve here but was also struggling with coming up with a good solution for macros. You could use helper methods and/or extensions to reduce the duplication in the original example, without introducing the rebuild issues. For instance https://gist.github.com/jakemac53/5b499747d0a8cc705e2fcf7598c8789c. |
I don't think this truly solves the issue, which is about flutter/flutter#51752 and variant topics like flutter_hooks. Maybe it's worth jumping on a call to talk about this? This is a topic close to my heart. I'd love to help coming up with a way to use macros to improve on this area. |
One of the use cases is simplifying build methods that today require significant nesting.
Here is one example: https://github.com/TimWhiting/local_widget_state_approaches/blob/master/lib/stateful/animated_counter.dart
The desired result is the equivalent of the single expression in the build method in that file. Some developers, however, find the large expression unintutive and would prefer something like:
Ideally the translation would not cause more rebuilds than today (so e.g. it's not good if hoisting the TweenAnimationBuilder up to the top causes the Scaffold to rebuild whenever the counters change -- in the original, the whole build method in fact only runs once, only the builders rerun).
The current API doesn't really provide any hooks that I can see for this kind of change.
The text was updated successfully, but these errors were encountered: