BREAKING CHANGE: feat: use record struct for OneOf

This commit is contained in:
Kyle Ratti 2024-05-03 17:07:44 -04:00
parent c61590f26f
commit 5504a765dc
No known key found for this signature in database
GPG Key ID: 4D429B6287C68DD9

View File

@ -2,14 +2,18 @@
namespace FruityFoundation.Base.Structures; namespace FruityFoundation.Base.Structures;
public class OneOf<T1, T2> public readonly record struct OneOf<T1, T2>
{ {
private Maybe<T1> _itemOne { get; } private readonly Maybe<T1> _itemOne;
private Maybe<T2> _itemTwo { get; } private readonly Maybe<T2> _itemTwo;
public T1 ItemOne => _itemOne.OrThrow($"{nameof(ItemOne)} must have a value in order to be accessed"); public T1 ItemOne => _itemOne.OrThrow($"{nameof(ItemOne)} must have a value in order to be accessed");
public T2 ItemTwo => _itemTwo.OrThrow($"{nameof(ItemTwo)} must have a value in order to be accessed"); public T2 ItemTwo => _itemTwo.OrThrow($"{nameof(ItemTwo)} must have a value in order to be accessed");
/// <summary>
/// Exposes the underlying object as an object.
/// This is useful for pattern matching from C#.
/// </summary>
public object? RawItem public object? RawItem
{ {
get get
@ -46,14 +50,18 @@ public class OneOf<T1, T2>
public class OneOf<T1, T2, T3> public class OneOf<T1, T2, T3>
{ {
private Maybe<T1> _itemOne { get; } private readonly Maybe<T1> _itemOne;
private Maybe<T2> _itemTwo { get; } private readonly Maybe<T2> _itemTwo;
private Maybe<T3> _itemThree { get; } private readonly Maybe<T3> _itemThree;
public T1 ItemOne => _itemOne.OrThrow($"{nameof(ItemOne)} must have a value in order to be accessed"); public T1 ItemOne => _itemOne.OrThrow($"{nameof(ItemOne)} must have a value in order to be accessed");
public T2 ItemTwo => _itemTwo.OrThrow($"{nameof(ItemTwo)} must have a value in order to be accessed"); public T2 ItemTwo => _itemTwo.OrThrow($"{nameof(ItemTwo)} must have a value in order to be accessed");
public T3 ItemThree => _itemThree.OrThrow($"{nameof(ItemThree)} must have a value in order to be accessed"); public T3 ItemThree => _itemThree.OrThrow($"{nameof(ItemThree)} must have a value in order to be accessed");
/// <summary>
/// Exposes the underlying object as an object.
/// This is useful for pattern matching from C#.
/// </summary>
public object? RawItem public object? RawItem
{ {
get get
@ -108,4 +116,4 @@ public class OneOf<T1, T2, T3>
public static implicit operator OneOf<T1, T2, T3>(T1 oneOf) => new(oneOf, default, default); public static implicit operator OneOf<T1, T2, T3>(T1 oneOf) => new(oneOf, default, default);
public static implicit operator OneOf<T1, T2, T3>(T2 oneOf) => new(default, oneOf, default); public static implicit operator OneOf<T1, T2, T3>(T2 oneOf) => new(default, oneOf, default);
public static implicit operator OneOf<T1, T2, T3>(T3 oneOf) => new(default, default, oneOf); public static implicit operator OneOf<T1, T2, T3>(T3 oneOf) => new(default, default, oneOf);
} }