namespace SessionCompanion.ApiReturn { using System; public class Either { private readonly TL left; private readonly TR right; private readonly bool isLeft; public Either(TL left) { this.left = left; this.isLeft = true; } public Either(TR right) { this.right = right; this.isLeft = false; } public T Match(Func leftFunc, Func rightFunc) { if (leftFunc == null) { throw new ArgumentNullException(nameof(leftFunc)); } if (rightFunc == null) { throw new ArgumentNullException(nameof(rightFunc)); } return this.isLeft ? leftFunc(this.left) : rightFunc(this.right); } /// /// If right value is assigned, execute an action on it. /// /// Akcja do wykonania public void DoRight(Action rightAction) { if (rightAction == null) { throw new ArgumentNullException(nameof(rightAction)); } if (!this.isLeft) { rightAction(this.right); } } public TL LeftOrDefault() => this.Match(l => l, r => default(TL)); public TR RightOrDefault() => this.Match(l => default(TR), r => r); public static implicit operator Either(TL left) => new Either(left); public static implicit operator Either(TR right) => new Either(right); } }