Generalized async return types — it is a new C#7 feature that allows using not only Task as a return type of async methods but also other types (classes or structures) that satisfy some specific requirements.
At the same time, async/await is a way to call a set of "continuation" functions inside some context which is an essence of another design pattern — Monad. So, can we use async/await to write a code which will behave in the same way like if we used monads? It turns out that — yes (with some reservations). For example, the code below is compilable and working:
async Task Main()
{
foreach (var s in new[] { "1,2", "3,7,1", null, "1" })
{
var res = await Sum(s).GetMaybeResult();
Console.WriteLine(res.IsNothing ? "Nothing" : res.GetValue().ToString());
}
// 3, 11, Nothing, Nothing
}
async Maybe<int> Sum(string input)
{
var args = await Split(input);//No result checking
var result = 0;
foreach (var arg in args)
result += await Parse(arg);//No result checking
return result;
}
Maybe<string[]> Split(string str)
{
var parts = str?.Split(',').Where(s=>!string.IsNullOrWhiteSpace(s)).ToArray();
return parts == null || parts.Length < 2 ? Maybe<string[]>.Nothing() : parts;
}
Maybe<int> Parse(string str)
=> int.TryParse(str, out var result) ? result : Maybe<int>.Nothing();
Further, I will explain how the code works...