dart-lang / macros

A Dart mono-repo for macro development.
BSD 3-Clause "New" or "Revised" License
54 stars 5 forks source link

Support manual code on top of model types #6

Closed davidmorgan closed 3 months ago

davidmorgan commented 3 months ago

Doc comments and methods.

davidmorgan commented 3 months ago

I experimented with this a bit. @jakemac53

Extension types implementing other extension types doesn't help much: you have to repeat the constructor in the manually written code, and it's not super pretty.

Augmentations for extension types don't seem to work fully, and it's also not super pretty, as the resulting type doesn't end up in one file.

An approach that I've used before is to automatically merge the generated code into the manually written code. It sounds horrible but I've seen it work pretty well, and it gives the best possible code; given that this is a user-facing package it seems worth considering :)

Here is an experiment along those lines, it generates then finds existing classes in the already-present file and replaces them, then appends any new ones to the end:

https://github.com/dart-lang/macros/commit/16dba5040b5f0c761f553290694ea8b8a49ee726

Manually added members are marked with a comment // End of generated members., everything after that point in the type is left alone. It doesn't delete unwanted extension types yet, it should :) ... and then maybe an // End of generated types. comment to allow manually-written types.

With the version linked you can write comments for types manually, but not yet comments for generated members. We should either generate them from descriptions or let you add them manually or maybe some mix.

What do you think? :)

jakemac53 commented 3 months ago

What about extensions on extension types :D

void main() {
  print(Thing('hello').plus(' world')); // Works!
}

extension type Thing(String x) {}

extension on Thing {
  String plus(String other) => '$this$other';
}
davidmorgan commented 3 months ago

Oh, yeah, that works :)

And is easier to maintain than some hack with merging, which we can always switch to later if we think it's important.

Then, we'll want to pull comments from the schema as you suggested originally, sent #11 for that. Thanks!