rearch-dart
rearch-dart copied to clipboard
feat: add flutter and widget related macros
Partially fixes #14
Macro execution doesn't appear to work in Flutter yet, but this is working in Dart-only for basic cases as far as I can tell.
Only thing that is broken is default values for named parameters, but those appear to be unimplemented by the macro system at the moment anyways.
Initial comments are that the macro API is a bit of a pain to work with; why is there no pattern matching on TypeAnnotation/why do I manually need to (typeAnnotation as NamedTypeAnnotation)? Also, I can't see any clearly defined way to check equality between two types (where one is defined externally), so I opted instead for String equality right now (which is obviously "wrong," but will work for 99.9% of cases).
They should really incorporate an api like: builder.resolve(myType) == Widget.staticType or similar, where Widget is imported from package:flutter/widgets.dart
The widget macros, which were the scope of this PR, are essentially done, at least to the best of what I can do right now with the current macros implementation. Just need to wait until macros are implemented further before I can clean this up more.
Looks like quite a bit changed in the macros implementation since the last actual commit--need to bring this up to speed again.
Updated this yet once again; added in some cool quasi-quasiquoting (get it?) functionality to make writing the macros suck less. See the extension _Substitute on String if you're making your own macros.
This time I had to test in packages/rearch since Flutter support isn't there yet. For future reference, needed this type boilerplate:
abstract class BuildContext {
T dependOnInheritedWidgetOfExactType<T>();
}
abstract class Widget {
const Widget({Object? key,});
}
abstract class WidgetHandle {}
abstract class RearchConsumer extends Widget {
const RearchConsumer({super.key,});
Widget build(BuildContext context, WidgetHandle use);
}
abstract class InheritedWidget extends Widget {
const InheritedWidget({required Widget child,super.key,});
}
extension A on WidgetHandle {
(T, void Function(T)) state<T>(T init) => throw '';
}
https://github.com/dart-lang/language/issues/1482#issuecomment-2622895490