-
Notifications
You must be signed in to change notification settings - Fork 17
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
Factory Methods #12
Comments
I think this is a great idea. It would also work nicely when we add generic support since we cannot infer generic types with constructors. |
I think #17 addresses this use case with a more superior implementation due to more control over the union members' types. What do you think @mumby0168 ? |
Hi yes I like this, no reason the root record, the Result in the example cannot contain factory methods also. I.e Results.Ok() with the need of the new keyword, it could follow the same pattern with a bool to indicate you want the extra methods generating. What do you think? |
I'm having a hard time seeing a generated factory method like |
Preliminary PR for this here: #33 |
After some more analysis, I'm going to defer this feature for the following reasons:
// No need for constructor or factory methods to instantiate union values.
StringOrNumber str = "foo";
StringOrNumber num = 123;
[Union]
partial record StringOrNumber
{
partial record Str(string Value);
partial record Num(double Value);
} If the community remains interested in this feature, I would whole-heartedly support a separate package containing this functionality. I'd also be glad to link to it from Dunet's readme if that becomes the case. |
I was thinking it might be quite nice to have an opt-in feature that makes you rely on factory methods to create the different types within a union. I was thinking this could be another attribute maybe
[UnionWithFactory]
or something like[Union(factory: true)]
.I took the code example from the samples and modified it to show how the factory methods could be used:
What do you think?
The text was updated successfully, but these errors were encountered: