Skip to content
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

Match on a specific union value only #103

Closed
domn1995 opened this issue Jan 6, 2023 · 0 comments · Fixed by #113
Closed

Match on a specific union value only #103

domn1995 opened this issue Jan 6, 2023 · 0 comments · Fixed by #113
Labels
enhancement New feature or request good first issue Good for newcomers

Comments

@domn1995
Copy link
Owner

domn1995 commented Jan 6, 2023

Some times we just want to do something given a specific union value, rather than handling all cases explicitly. This is most useful on larger unions.

For example consider the following code:

[Union]
public partial record struct MathResult<T>
    : where T : INumber<T>
{
    public partial record struct Infinity;
    public partial record struct Success(T Value);
    public partial record struct NaN;
    public partial record struct Undefined;
}

MathResult<double> result = // Some complex math calculation.

Some ways we might want to interact with result

  • With a dedicated Match method for each case?
// A `MatchX()` is generated for each member `X` of the union. You must provide an else case.
var value = result.MatchSuccess(
    // If `Infinity, NaN, or Undefined` return 0.
    () => 0
);
  • Perhaps a more imperative, idiomatic C# way?
// A `TryGetX` is generated for each member `X` of the union.
if (result.TryGetSuccess(out var success))
{
    Console.WriteLine(success.Value);
}
@domn1995 domn1995 added enhancement New feature or request good first issue Good for newcomers labels Jan 6, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant