dart-lang / language

Design of the Dart language
Other
2.61k stars 200 forks source link

Adding conditional imports/parts with macros. #3916

Open lrhn opened 1 month ago

lrhn commented 1 month ago

Macros can add imports, and possibly also conditional imports (or they can import a library which has conditional exports).

If it's also possible to have conditional parts, which it should be because it's very useful for have your platform-specific code to have access to privates, then it becomes a question whether macros can add conditional part files (or sub-part-files at all).

The current model seems to be that macros are combined into a single augmentation file, really only because that makes the augmentations more easily readable while debugging. It would also be possible for each macro to generate one new part file per phase, all appended at the end of the part-directive list, instead of just one file. That should give the same augmentation application order.

If a macro generated file can add a sub-part-file, at least one with no imports, then that can be flattened into a single macro generated file too, by just appending it at the end of the file. If the sub-part-file has imports, that flattening won't work, and if the sub-part-file declaration is conditional, then it definitely won't work.

In short:

jakemac53 commented 1 month ago

For now at least, I don't think it should be allowed. We have enough complexity already. Possibly in the future we evaluate it, if the need arises. For platform specific code my hope would be that most use cases can push the platform specific code to a shared library instead, which macro generated code uses.

davidmorgan commented 4 weeks ago

Agreed.

I think we can leave enough space in the host<->macro protocol that this is the kind of improvement we can make later.