static Res<User> TryGetUser() { .. }
static long PutUserToDbGetId(User user) {
// method that writes the user to a database table and returns back the auto-generated id/primary-key
// might fail and throw!
}
Res<long> id = TryGetUser().TryMap(PutUserToDbGetId);
// equivalently:
Res<long> id = TryGetUser().TryMap(user => PutUserToDbGetId(user));
// Res<long> id will be:
// - Err(called on Err) when TryGetUser returns Err,
// - Err(relevant error message) when TryGetUser returns Ok(user) but the database transaction throws an exception,
// - Ok(id) when TryGetUser returns Ok(user), the database transaction succeeds and returns the auto-generated id.
// it provides a shorthand for the following verbose/unpleasant version:
Opt<User> user = TryGetUser();
Res<long> id;
if (user.IsNone)
id = Err<long>("no user");
else
{
try
{
id = Ok(PutUserToDb(user.Unwrap()));
}
catch (Exception e)
{
id = Err<long>("db-operation failed, check the exception message: " + e.Message);
}
}
public Res<TOut> TryMap<TOut>(
Func<T, TOut> map,
string name = ""
)
[Missing <typeparam name="TOut"/> documentation for "M:Orx.Fun.Result.Res`1.TryMap``1(System.Func{`0,``0},System.String)"]