4 ms·
Here is a moral equivalent in C#: public abstract class Maybe<T> { public abstract T Value { get; } public virtual string Failure =
by noblethrasher 8y ago
Here is a moral equivalent in C#:
public abstract class Maybe<T>
{
public abstract T Value { get; }
public virtual string Failure => null;
public SelectMany<U, V>(Func<T, Maybe<U>> f, Func<T, U, V> g)
{
var that = f(this.Value);
if(that.Failure != null)
return new Failed(that.Failure);
return new Success(g(this.Value, that.Value));
}
public sealed class Success : Maybe<T>
{
public override Value { get; }
public Success(T value) => this.Value = value;
}
public sealed class Failed : Maybe<T>
{
public override Failure { get; }
public override Value => throw new InvalidOperationException();
public Failed(string s) => this.Failure = s;
}
}
Maybe<string> UpdateCustomerWithErrorHandling()
{
return
from req in receiveRequest();
from v in validateRequest(req)
from c in canicalizeEmail(req)
from d in db.updateDbFromRequest(req)
from e in smptServer.sendEmail(req.Email)
select "OK";
}
- noblethrasher 8y agoActually, the above code doesn't compile (I just dashed it off while waiting for a real world project to compile). Valid C#: https://gist.github.com/noblethrasher/2d0e99f86b9853c646f3fa91c59b2223 https://gist.github.com/noblethrasher/2d0e99f86b9853c646f3fa...
- DenisM 8y agoIs that the same concept here: https://ericlippert.com/2013/04/02/monads-part-twelve/ https://ericlippert.com/2013/04/02/monads-part-twelve/