-
Notifications
You must be signed in to change notification settings - Fork 206
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
Macros: Allow anonymous classes/functions for the sake of being used by generated code #3817
Comments
Why not just make the function private? Macro code doesn't have capabilities not available to user code - so there needs to be some identifier for this function that a macro, or any other code in the library, can refer to. |
Making the function private isn't enough.
Users may want to generate a public widget, or a private widget. At the moment, the way I've done it is require users to use @widget
Widget _foo(...); // generates Foo
@widget
Widget __foo(..); // Generates _Foo But that's super awkward IMO. |
I could imagine an annotation + lint that ensures user code doesn't use a member, even within the same library? The macro could add the annotation (augmentations do allow this, even if we don't have an API for it today in macros). You could even just add
That is pretty awkward for sure, I could imagine a |
There are possible workarounds, of course. But I think they are less ideal overall. There's also the case of renaming. With such an approach, renaming |
Interesting, so would do initiate the rename from the Identifier in the macro annotation |
Indeed. There's also the case where the renaming is performed in a different place: @Macro(Example)
Widget () => ...;
...
@override
Widget build(context) {
// We could rename `Example` here, and it would rename `@Macro(Example)` accordingly,
// as this is the same identifier.
return Example(...);
} |
An alternative syntax could be: @Macro(Example, () {
return Container();
}); But we'd need to support declaring macros that's not attached to an element. Although the syntax is a bit less pleasing to the eye IMO. And I think it'd be valuable to be able to define return values ; which closures lack. |
Going back to this, this could solve another problem with macro composition: Users will regularly want to apply macros on the generated identifier. Take this functional_widget issue as example: rrousselGit/functional_widget#119 When users define: @stateless
Widget fn(context) => ... Some may want to generate: @MyMacro()
class Fn extends StatelessWidget {...} Using this proposal, we could consider supporting specifying annotations on @Stateless(
@MyMacro()
Example
)
Widget () => ...; The |
Why not: // @MyOtherMacro()
@Stateless()
Widget Example() => Container(...); The top-level function
So instead of changing the lexical structure of the language (which IMHO seems awkward to change) to fulfill this need, we could support transforming functions to classes with macros (I don't know if this is possible though, just throwing ideas). |
I suggested that before. But there's quite a bit of pushback against the idea of changing the prototype of objects in breaking way. For example, I wanted macros to be able to add new parameters to functions, instead of having to generate a function. But it's pretty clear the team doesn't want that :P |
Hello!
With macros coming around, it's likely going to be common for folks to define a class/function that is never meant to be used directly.
Take the idea of a macro that generates a Widget from a function:
In this scneario, we never want to use the
example
function. Rather, we'll want to use the generatedExample
class.As such, this is both error prone and pollutes the public API.
Proposal: Allow functions/classes to be unnamed, and referenced by macros
We could allow functions to not have a name.
Then, rather than macro rely on the function name to generate the class name, we could pass the desired named as input to the macro:
This way, the function is no-longer directly usable. Users of the macro will have to use it through the generated
Example
class.The text was updated successfully, but these errors were encountered: