dart-lang / code_builder

A fluent API for generating valid Dart source code
https://pub.dev/packages/code_builder
BSD 3-Clause "New" or "Revised" License
427 stars 66 forks source link

Add support for extension types #399

Closed joshualitt closed 10 months ago

joshualitt commented 1 year ago

I'd like to use inline classes in package:code_builder.

AFAICT there are two approaches we can take:

  1. Just add a bit to ClassBuilder.
  2. Create an entirely new builder just for inline classes.

(1) is more straightforward, and perhaps more discoverable for users. But inline classes are a bit more restricted than regular classes, so I can definitely see an argument for (2).

Thoughts?

CCing @kevmoo @natebosch @matanlurey for feedback.

kevmoo commented 1 year ago

My initial thought: this package isn't the analyzer. If folks generate invalid code, they will find out statically or at runtime pretty quick.

If it's 5% more work to make a separate thing, consider it. If it's 50% more work – or tough to discover, I'd just add a flag to the normal class.

Just my opinion. Happy to hear thoughts from others...

natebosch commented 1 year ago

Agreed that we don't need to spend too much effort on keeping usage on the rails.

I don't have a strong opinion on whether it's a new class or a field on ClassBuilder.

joshualitt commented 1 year ago

Okay, if people don't have strong feelings, and validation isn't that important, than I think it is easier to just make this a bit.

The only argument against making it a bit is that it could provide better validation for users. However, the cost of that benefit is pretty substantial. In addition to being more work to land the feature, it'd also be more work to maintain. Furthermore, there are other class modifiers, i.e. sealed, and those really only make sense as bits. Having inline be the odd modifier, even if some of the differences between inline and non-inline classes are pretty substantial, will probably be confusing to users.

Anyway, we have some time to think about this. I'll try and find the time to put together a CL in the next few weeks and we can discuss things further there.

srujzs commented 10 months ago

I'm going to likely take a crack at this so that we can migrate package:web to extension types. I haven't dug into this package too much to determine if we should just modify ClassBuilder, but extension types are a sufficiently different feature that I'm going to investigate having a separate builder. We've also basically finalized the syntax for extension types, so now's a good time to add this to the package.