-
Notifications
You must be signed in to change notification settings - Fork 213
Static Metaprogramming #1482
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
Comments
Would static function composition be in the scope of this feature? At the moment, higher-order functions in Dart are fairly limited since they require knowing the full prototype of the decorated function. An example would be a void Function() debouce(Duration duration, void Function() decorated) {
Timer? timer;
return () {
timer?.cancel();
timer = Timer(duration, () => decorated());
};
} which allows us to, instead of: class Example {
void doSomething() {
}
} write: class Example {
final doSomething = debounce(Duration(seconds: 1), () {
});
} but that comes with a few drawbacks:
With static meta-programming, our class Example {
@Debounce(Duration(seconds: 1))
void doSomething() {
print('doSomething');
}
@Debounce(Duration(seconds: 1))
void doSomethingElse(int value, {String named}) {
print('doSomethingElse $value named: $named');
}
} |
There is a delicate balance re: static function composition, but there are certainly many useful things that could be done with it. I think ultimately it is something we would like to support as long as we can make it obvious enough that this wrapping is happening. The specific balance would be around user confusion - we have a guiding principle that we don't want to allow overwriting of code in order to ensure that programs keep their original meaning. There are a lot of useful things you could do by simply wrapping a function in some other function (some additional ones might include uniform exception handling, analytics reporting, argument validation, etc). Most of these things would not change the meaning really of the original function, but the code is being "changed" in some sense by being wrapped. Ultimately my sense is this is something we should try to support though. I think the usefulness probably outweighs the potential for doing weird things. |
I like Lisp approach (in my opinion, the utmost language when it comes to meta-programming). Instead of defining a |
For something like debounce, a more aspect-like approach seems preferable. Say, if you could declaratively wrap a function body with some template code: class Example {
void doSomething() with debounce(Duration(seconds: 1)) {
print('doSomething');
}
void doSomethingElse(int value, {String named}) with debounce(Duration(seconds: 1)) {
print('doSomethingElse $value named: $named');
}
}
template debounce<R>(Duration duration) on R Function {
template final Stopwatch? sw;
template late R result;
if (sw != null && sw.elapsed < duration) {
return result;
} else {
(sw ??= Stopwatch()..start()).reset();
return result = super;
}
} This defines a "function template" (really, a kind of function mixin) which can be applied to other functions. (Maybe we just need AspectD for Dart.) |
But an important part of function composition is also the ability to inject parameters and ask for more parameters. For example, a good candidate is functional stateless-widgets, to add a @statelessWidget
Widget example(BuildContext context, {required String name}) {
return Text(name);
} and the resulting prototype after composition would be: Widget Function({Key? key, required String name}) where the final code would be: class _Example extends StatelessWidget {
Example({Key? key, required String name}): super(key: key);
final String name;
@override
Widget build(BuildContext) => originalExampleFunction(context, name: name);
}
Widget example({Key? key, required String name}) {
return _Example(key: key, name: name);
} |
I definitely agree we don't want to allow for changing the signature of the function from what was written. I don't think that is prohibitive though as long as you are allowed to generate a new function/method next to the existing one with the signature you want. The original function might be private in that case. |
That's what functional_widget does, but the consequence is that the developer experience is pretty bad. A major issue is that it breaks the "go to definition" functionality because instead of being redirected to their function, users are redirected to the generated code It also causes a lot of confusion around naming. Because it's common to want to have control on whether the generated class/function is public or private, but the original function to always be private. By modifying the prototype instead, this gives more control to users over the name of the generated functions. |
Allowing the signature to be modified has a lot of disadvantages as well. I think its probably worse to see a function which is written to have a totally different signature than it actually has, than to be navigated to a generated function (which you can then follow through to the real one). You can potentially blackbox those functions in the debugger as well so it skips right to the real one if you are stepping through. |
I suppose this will allow generating |
Yes. |
@tatumizer This issue is just for the general problem of static metaprogramming. What you describe would be one possible solution to it, although we are trying to avoid exposing a full AST api because that can make it hard to evolve the language in the future. See https://github.com/dart-lang/language/blob/master/working/static%20metaprogramming/intro.md for an intro into the general design direction we are thinking of here which I think is not necessarily so far off from what you describe (although the mechanics are different). |
Great intro & docs. Hopefully we'll stay (far far) away from annotations to develop/work with static meta programming?! |
The main reason we use this as an example is its well understood by many people, and it is also actually particularly demanding in terms of features to actually implement due to the public api itself needing to be generated :).
Can you elaborate? Default values for parameters are getting some important upgrades in null safe dart (at least the major loophole of being able to override them accidentally by passing |
I believe the issue is that we cannot easily differentiate between freezed supports this, but only because it relies on factory constructors and interface to hide the internals of |
Right, this is what I was describing which null safety actually does fix at least partially. You can make the parameter non-nullable (with a default), and then null can no longer be passed at all. Wrapping functions are required to copy the default value, basically it forces you to explicitly handle this does cause some extra boilerplate but is safe. For nullable parameters you still can't differentiate (at least in the function wrapping case, if they don't provide a default as well) |
Metaprogramming is a broad topic. How to rationalize? We should start with what gives the best bang for buck (based on use cases). Draft topics for meta programming 'output' code:
Also on output code:
Would be great if this could work without saving the file, a IDE-like syntax (hidden) code running continuously if syntax is valid. I refuse to use build_runner's |
Metaprograming opens doors to many nice features Other language that does a great job at implementing macros is Haxe you can use Haxe language to define macros I guess there are many challenges to implement this. |
can we extend classes with analyzer plugin? |
I'm not sure if I like the idea having this added to Dart because the beauty of Dart is its simplicity. The fact that it isn't as concise as other languages it in reality an advantage because it makes Dart code really easy to read and to reason about. |
I agree with this. |
@escamoteur Writing less code does not make it more complicated necessarily. It can, I agree, if someone does not fully understand the new syntax. But the trade-off is obvious: time & the number of lines saved vs the need for someone to learn a few capabilities. Generated code is normal simple code. I just suggested real-time code generation instead of running the builder every time or watching it to save. That way you get real time goto. But if you are using notepad then of course you need to run a process. |
Just to be 100% clear, we are intensely focused on these exact questions. We will not ship something which does not integrate well with all of our tools and workflows. You should be able to read code and understand it, go to definition, step through the code in the debugger, get good error messages, get clear and comprehensible stack traces, etc. |
In my honest opinion: things must be obvious, not magical.
^ this |
But there is nothing beautiful about writing data classes or running complicated and and slow code-generation tools. I'm hoping this can lead to more simplicity not less. Vast mounds of code will be removed from our visible classes. StatefulWidget can maybe just go away? (compiler can run the split macro before it builds?). Things can be auto-disposed. Seems like this could hit a lot of pain points, not just data classes and serialization.. |
Since dart currently offers code generation for similar jobs-to-be-done, I'd suggest evaluating potential concerns with that consideration:
On the other hand, besides being an upgrade from codegen for developers, metaprogramming could provide healthier means for language evolution beyond getting data classes done. Quoting Bryan Cantrill:
PS @jakemac53 the |
this would be fantastic if it allowed, the longed-for serialization for JSON natively without the need for manual code generation or reflection in time of execution Today practically all applications depend on serialization for JSON, a modern language like dart should already have a form of native serialization in the language, being obliged to use manual codegen or typing serialization manually is something very unpleasant |
My approach on a macro mechanism. Basically tagging a certain scope with a macro annotation, that refers to one or multiple classes to 1:1 replace the code virtually... like a projection. It's very easy to understand and QOL can be extended by providing utility classes. #ToStringMaker() // '#' indicates macro and will rewrite all code in next scope
class Person {
String name;
int age;
}
// [REWRITTEN CODE] => displayed readonly in IDE
// class Person {
// String name;
// int age;
//
// toString() => 'Person(vorname:$name, age:$age)'
// }
class ToStringMaker extends Macro {
// fields and constructor can optionally obtain parameters
@override
String generate(String code, MacroContext context) { // MacroContext provides access to other Dart files in project and other introspection features
var writer = DartClassWriter(code); // DartClassWriter knows the structure of Dart code
writer.add('String toString() => \'${writer.className}(${writer.fields.map(field => '${field.name}:\${field.name}').join(', ')})\'');
return writer.code; // substitute code for referenced scope
}
} |
Interesting. I like the structure a lot, but not so much #ToStringMaker() (hashtag working as annotation). Syntax would be better if #ToStringMaker() could be incorporated into the class definition. |
I do know what the problem with |
JIT compilation is fast, even with the mirrors enabled. |
did we ever consider a declarative mechanism for macros? macro Data on T {
`T` copyWith({
for (final field in fields)
`field.type.nullable` `field.name` = this.`field.name`, // non-const defaults
}) => `primaryConstructor.autoCall(fields)`; // strawman
}
// basically, normal code, but anything wrapped in `` is dependent on introspection I know that a lot of code gen isnt so simple, but i would say that enough are. It just seems a bit strange to me why we seem so hard-set on raw string code gen |
@TekExplorer look very promising , is there any other language support somethings like this? |
This looks really good |
off the top of my head; rust's |
rust_rules are very complicated. |
Why is very complicated a bad thing? |
Oh, don't get me started on this. I've been a developer for 40 years (before retiring). And if I learned one thing over that time, it's that complexity kills. |
I hope I'm not adding noise in here. But since someone asked... defmodule LoggerExample do
for {i, level} <- %{1 => :err, 2 => :warn, 3 => :info} do
def log_level(unquote(i)), do: unquote(level)
end
end The above is (AFAIK!) unfeasible in Dart: for example, Elixir is dynamically typed (as of now 👀), Dart's not; and that's just the tip of the iceberg (analysis, macro debugging / exploration, type-safety, dartVM, hot reload, etc.). Indeed the team abandoned macros for a reason, and that is, behind these "simple" examples, there's A LOT of complexity. |
If you are looking for a prior art applicable to dart (more or less), Julia has it: module AnotherModule
export @show_value_user_and_module
orange = "bitter"
macro show_value_user_and_module(variable)
quote
println("The ", $(string(variable)), " you passed is ", $(esc(variable)),
" and the one from the module is ", $variable)
end
end
end
using .AnotherModule
@show_value_user_and_module orange
I haven't seen any design docs for build_runner, but I won't be surprised if the thing turns out to be as complicated (if not more) as the abandoned metaprogramming 😄 |
Challenge accepted ;) |
This is basically what was proposed in #1989, although the straw man syntax was very different. And yes, there are many languages that use this approach, notably famous is Lisp, where quoting |
@davidmorgan In addition to this list, I'd like to add this bug to the pile. Again, I'm sure addressing |
Thanks @lucavenir, I've marked it as a sub-issue of dart-lang/build#3806 |
You know, the biggest benefit macros provided was the live on-the-spot code gen But the actual API was... Not great. Is there any chance we could get just the instant gen and just leave the actual implementation and introspection to the ecosystem to refine on its own? Also, could I get an enumeration of the exact reasons the attempt failed as-is? Just so I don't re-tread covered grounds. Because sometimes I want to make a quick extension on a function type, multiple times, but not enough to want to pull out build_runner. |
I would also really like to know this, because if the problem is just performance, this could be improved over the years, seeing as the Rust compilation has improved over the years, in addition to better software optimizations, there are also hardware improvements over time. |
I think it might be interesting if the dart SDK (compiler/analyzer) had an extension plugin API with hooks so that the community could develop a macro package that could be coupled to the SDK with fewer barriers than build_runner, in a more integrated way so that a package that depends on it doesn't force the package user to have to put build_runner as a dependency and have to run dart run build_runner build, that is, so that this becomes transparent to the package user, eliminating this barrier would be very good and it would work almost like the native macros of the dart SDK. |
You can look through the closed issues tagged static-metaprogramming on the language repo, there were plenty of large open issues to choose from. The biggest functional piece missing was the capability to pass configuration to macros, e.g. Macros would have run as you type in the IDE, it simply does not make sense to release a macros or macros-like feature and promise it will be faster later :) |
i feel like extension types could have been great for that. also a lot of the issues are regarding api and analysis. i dont think we even "had" the feature for long enough to even care about that yet. I can easily imagine: macro class MyMacro {
// you could get the actual value, but this is more useful for emitting to straight to code. basically a Reviver'd version, by my understanding
final Literal<String> key;
// information, no coercion or anything
final String info;
// the type itself is useless, but as its a literal, it secretly retained the associated element, so we can still use it for type analysis
// possibly has a `.identifier` getter
final Literal<Type> supertype;
// possibly used for non-const superpowers, if we want the revivable, but dont care to read the value directly in the macro.
final Code</*ExpectedType*/> something;
}
Literal() is Code seems simple enough, but i'm not deep enough in implementation code to actually say that i suppose. |
It's very hard.
Codegen just fails if it asks for ASTs where they're not available, but that's not an option for a language feature. Codegen just breaks when ASTs change :) also not an option for a language feature. |
There's one key reason: complexity. It gives rise to a potentially infinite number of problems, each of which, in turn, ... etc. |
This seems like a bad idea. To me, the IDE should ignore macros and only the compiler should analyze and process the macros and generate code when compiling the code. Making the IDE analyze and generate code when typing is absurdly expensive. The user experience doesn't need to be magical. See, when you use very dynamic things, you only know for sure the result when running or compiling, and that's not the end of the world. You don't need to have the best UX in the world to be very useful. |
That's a great way to completely shut down analyzer-level type safety. How are you supposed to use a generated type otherwise? |
There is no miracle, there is always a compromise, there are always pros and cons in everything, it is not feasible to make the IDE analyze the entire project with each keystroke, that is madness, but having said that there is no loss of type safety once the compiler finds an error it will show it. |
And how is the ide meant to find an error for a type it doesn't yet know exists because it hasn't been macro-expanded? If perhaps we reanalyzed after we pause typing, or when we ask for it (configurable? On-save? Oh hi Hey, maybe we should go and abuse custom_lint quick-fixes as crappy macros! Heh. |
Metaprogramming refers to code that operates on other code as if it were data. It can take code in as parameters, reflect over it, inspect it, create it, modify it, and return it. Static metaprogramming means doing that work at compile-time, and typically modifying or adding to the program based on that work.
Today it is possible to do static metaprogramming completely outside of the language - using packages such as build_runner to generate code and using the analyzer apis for introspection. These separate tools however are not well integrated into the compilers or tools, and it adds a lot of complexity where this is done. It also tends to be slower than an integrated solution because it can't share any work with the compiler.
Sample Use Case - Data Classes
The most requested open language issue is to add data classes. A data class is essentially a regular Dart class that comes with an automatically provided constructor and implementations of
==
,hashCode
, andcopyWith()
(calledcopy()
in Kotlin) methods based on the fields the user declares in the class.The reason this is a language feature request is because there’s no way for a Dart library or framework to add data classes as a reusable mechanism. Again, this is because there isn’t any easily available abstraction that lets a Dart user express “given this set of fields, add these methods to the class”. The
copyWith()
method is particularly challenging because it’s not just the bodyof that method that depends on the surrounding class’s fields. The parameter list itself does too.
We could add data classes to the language, but that only satisfies users who want a nice syntax for that specific set of policies. What happens when users instead want a nice notation for classes that are deeply immutable, dependency-injected, observable, or differentiable? Sufficiently powerful static metaprogramming could let users define these policies in reusable abstractions and keep the slower-moving Dart language out of the fast-moving methodology business.
Design
See this intro doc for the general design direction we are exploring right now.
Update January 2025
We have an unfortunate update on macros/metaprogramming. We have invested significant time and resources to prototype macros over the past couple years. Unfortunately, each time we solved a major technical hurdle, we saw new ones pop up. At this point, we are not seeing macros converging anytime soon toward a feature we are comfortable shipping, with the quality and developer-time performance we want.
After considering the opportunity cost — in particular, the features we could be shipping to the community instead — we’ve made the difficult decision to stop our work on macros.
For additional details, please see the blog post:
https://medium.com/dartlang/an-update-on-dart-macros-data-serialization-06d3037d4f12
The text was updated successfully, but these errors were encountered: