Skip to content

Generate constant for types without arguments #14

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

Almighty-Alpaca
Copy link

@Almighty-Alpaca Almighty-Alpaca commented Dec 8, 2021

In my project I have a sealed class with an empty subtype that I use as default argument in a constructor. For that to work I need a static constant of that type, which I currently have to create myself as a top level field. This PR allows the writer to generate those constants as static const field inside the sealed class.

Edit: I've noticed that my first commit didn't produce correct Dart code, so I've fixed that in the second one. Unfortunately for that I needed to remove the factory method that was previously generated for such subtypes, which is a breaking change. If this isn't acceptable, this could be solved with an optional parameter on the @Sealed() annotation controlling the code generation. But before I go there, I'll wait for feedback if this is a welcome addition at all here first.

Excerpt of generated code:

abstract class Weather {
  static const Weather sunny = WeatherSunny();
  
  const factory Weather.rainy({required int rain}) = WeatherRainy;
}

@FatulM
Copy link
Contributor

FatulM commented Mar 30, 2024

Hi,
in the standard generated code,
we have:

abstract class Weather {
   const factory Weather.sunny() = WeatherSunny();
  
  const factory Weather.rainy({required int rain}) = WeatherRainy;
}

since we have constant factories,
so you can use:

void doSomething({Weather arg = const Weather.sunny()}) {}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants