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

A non-exception Value<T> alternative for unions #49

Open
gavinSteyn opened this issue Mar 6, 2018 · 2 comments
Open

A non-exception Value<T> alternative for unions #49

gavinSteyn opened this issue Mar 6, 2018 · 2 comments

Comments

@gavinSteyn
Copy link

I'd like to be able to get the Value of a union using the syntax of Value() (i.e. using the class name), but without the risk of throwing an exception. I have 3 approaches in mind, and before I start coding I was hoping you could weigh in on which way I should go.

  1. Add a Option<T> GetValue<T>() which returns Some if the union is of type T, else None
  2. Add a bool TryGetValue<T>(out value) which returns a bool as to whether it succeeded and the actual value in the out param
  3. Add a bool HasValue<T>() which just returns a bool for whether this union is of type T

Do you have a preferred approach?

Thank you,
Gavin Steyn

DavidArno added a commit that referenced this issue Nov 26, 2018
Added TryGetValue() methods to the union types. These return the value as a Option<T>.Some, or OPtion<T>.None if the type doesn't match any of the union cases.
@DavidArno DavidArno self-assigned this Nov 26, 2018
@DavidArno DavidArno added this to the V4.0 milestone Nov 26, 2018
DavidArno added a commit that referenced this issue Nov 26, 2018
This branch included the `HasValue` method for #49.
This branch also included the PR #50. But as `Maybe<T>` is now removed in the v8 branch, much of that PR becomes redundant and so has been removed.
DavidArno added a commit that referenced this issue Nov 26, 2018
… how to use git properly)

This branch included the `HasValue` method for #49.
This branch also included the PR #50. But as `Maybe<T>` is now removed in the v8 branch, much of that PR becomes redundant and so has been removed.
@DavidArno
Copy link
Owner

DavidArno commented Aug 30, 2019

Putting a checklist here to keep track of adding this to the new v4:

  • Use the names CaseOf<T>(), TryCaseOf<T>() and HasCaseOf<T> to better fit with other such methods.
  • Add a HasCase(Variant variant) method too
  • Add to Union<T1, T2>
  • Add to Union<T1, T2, T3>
  • Add to Union<T1, T2, T3, T4>
  • Document

@DavidArno
Copy link
Owner

Need to add details of these methods to the wiki now that v4 is released.

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

No branches or pull requests

2 participants