using System; public abstract class Option { public Option Map(Converter converter) { if (this.IsSome) { return new Some(converter(this.Value)); } return new None(); } public abstract T Value { get; } public abstract Boolean IsSome { get; } public abstract Boolean IsNone { get; } public static Option None { get; } = new None(); } public static class Option { public static Option Some(T value) => new Some(value); } public sealed class None : Option { public override T Value { get { throw new NotSupportedException("No Value in None."); } } public override Boolean IsSome => false; public override Boolean IsNone => true; } public sealed class Some : Option { public Some(T value) { if (value == null) { throw new ArgumentNullException(nameof(value), "The Value in Some cannot be null."); } this.Value = value; } public override T Value { get; } public override Boolean IsSome => true; public override Boolean IsNone => false; }