If you have some time I'd like to hear what motivated your design decision to use the OptionType Tag and the associated conditional logic in the C# Option implementation. Seeing the "Tag" made my "refactoring sense tingle" , in particular
*
Replace Type Code with Subclasses
*
Replace conditional with Polymorphism
In particular, are there any shortcomings in the following alternative implementation that I am overlooking?
[pre]
using System;
namespace AltOption {
public abstract class Option<T> {
protected Option() {}
public abstract bool MatchNone();
public abstract bool MatchSome(out T value);
public abstract Option<R> Map<R>(Func<T, R> f );
public abstract Option<R> Bind<R>(Func<T, Option><R>> f);
}
public class None<T> : Option<T> {
public override bool MatchNone() { return true; }
public override bool MatchSome(out T value) {
value = default(T);
return false;
}
public override Option<R> Map<R>( Func<T, R> f ) {
return new None<R>();
}
public override Option<R> Bind<R>( Func<T, Option><R>> f) {
return new None<R>();
}
}
public class Some<T> : Option<T> {
public Some(T value) {
Value = value;
}
public T Value { get; private set; }
public override bool MatchNone() { return false; }
public override bool MatchSome(out T value) {
value = Value;
return true;
}
public override Option<R> Map<R>( Func<T, R> f ) {
return new Some<R>(f(Value));
}
public override Option<R> Bind<R>( Func<T, Option><R>> f ){
return f(Value);
}
}
public static class Option {
public static Option<T> None<T>() {
return new None<T>();
}
public static Option<T> Some<T>(T value) {
return new Some<T>(value);
}
}
}
[/pre]
Thank You.