-
-
Notifications
You must be signed in to change notification settings - Fork 236
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
All properties available on all factory methods #60
Comments
That is currently not supported. A more complex case I would like to fully support is: abstract class Complex with _$Complex {
const factory Complex.a(int value) = ComplexA;
const factory Complex.b(double value) = ComplexB;
} I'm thinking of a few things for these cases: Complex example = Complex.a(42);
num value = example.value; // fine
double value = example.value // implicit-cast error, value is a num not a double
Then one more difficult scenario would be: abstract class ReallyComplex with _$ReallyComplex {
const factory ReallyComplex.a(int variable) = ReallyComplexA;
const factory ReallyComplex.b(double variable) = ReallyComplexB;
const factory ReallyComplex.c() = ReallyComplexC;
} In which case I think I'd go with: ReallyComplex example;
example.$variable(
(num variable) => print(variable),
orElse: () => print('nothing'),
); The ReallyComplex example;
if (example is ReallyComplexA) {
print(example.value);
} |
what would that look like for my example?
and if I wanted to differentiate between continue and retry I would have to vary my type (ActionContinue or ActionRetry or something like that)
...if I have that right then I think that would work well. It would be nice to vary by the continue and Retry types though and not by the parameters. In my example an Action is always an Action but the Result is the type that determines whether an action should Continue or Retry), maybe that is impossible as everything works around the parameters and not the types? but, if the syntax is possible, maybe an
|
An alternative is to make the properties that are not shared between all constructors nullable. Since Dart will "soon" have non-nullable types, it should be compile safe. Freezed could still generate that |
I haven't thought this through that much (!) but could you go for a ducktyping approach? In my example above all I really want to do is run code wherever I have a property called action of type Action.
vary by the property names rather than the types because an |
For this specific case, we just have to add an abstract getter with the nearest common type : abstract class Complex with _$Complex {
const factory Complex.a(int value) = ComplexA;
const factory Complex.b(double value) = ComplexB;
num get value;
} |
Closing in favor of #907, which enables more advanced class hierarchies. Alternatively, you can use |
(great job Remi!)
I wasn't sure how to title this! It isn't possible to do the following because the properties are only available if all factory methods have that property. How is it best to overcome this?
I thought that composition might do it, is that the best way? Is it just a different way of thinking than my inheritance conditioned brain?
The text was updated successfully, but these errors were encountered: