using System.Collections;
using System.Collections.Generic;
public static class ObjectExtensions
public static void Match<T>(this Option<T> opt, Action<T> good, Action bad) {
public static V Match<T,V>(this Option<T> opt, Func<T,V> good, Func<V> bad) =>
opt.isSome()?good(opt.Content[0]) : bad();
public class Option<T> : IEnumerable<T>
public T[] Content { get; }
private Option(T[] content)
public static Option<T> Some(T value) => new Option<T>(new[] {value});
public static Option<T> None() => new Option<T>(new T[0]);
public IEnumerator<T> GetEnumerator() =>
((IEnumerable<T>) this.Content).GetEnumerator();
IEnumerator IEnumerable.GetEnumerator() =>
public bool isSome()=>this.Content.Length!=0;
public Car(string name){_name = name;}
public override string ToString() => _name;
private DateTime BirthDate { get; }
public Person(DateTime birthDate)
this.BirthDate = birthDate.Date;
public Option<Car> TryGetCar(DateTime at)
if (this.BirthDate.AddYears(18) <= at)
return Option<Car>.Some(new Car("Toyota"));
return Option<Car>.None();
public static void Main()
var p = new Person(DateTime.Now.AddYears(-2));
p = new Person(DateTime.Now.AddYears(-20));
var car = p.TryGetCar(DateTime.Now);
Console.WriteLine(car.Match(a=>a.ToString(), ()=>"you are minor"));