SuccincT icon indicating copy to clipboard operation
SuccincT copied to clipboard

A non-exception Value<T> alternative for unions

Open gavinSteyn opened this issue 7 years ago • 2 comments

I'd like to be able to get the Value of a union using the syntax of Value<T>() (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

gavinSteyn avatar Mar 06 '18 22:03 gavinSteyn

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

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

DavidArno avatar Aug 30 '19 10:08 DavidArno

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

DavidArno avatar Feb 17 '20 10:02 DavidArno