Lazy counterpart of OkIf(Boolean, String) where condition is evaluated only if this is Ok.
Returns back the Err if this is Err. Otherwise, returns Ok if lazyOkCondition holds; Err if it does not hold. Especially useful in fluent input validation.static Res<User> Login(string username, string passwordHash)
{
return OkIf(!string.IsNullOrEmpty(username)) // validate username
.OkIf(!string.IsNullOrEmpty(passwordHash)) // validate password-hash
.OkIf(() => userRepo.ContainsKey(username)) // further validate user; assume this is an expensive call, so we prefer the lazy variant
.Map(() => GetUser(username, password)); // finally map into actual result;
// any Err in validation steps will directly be mapped to Err, avoiding GetUser call.
}
public Res OkIf(
Func<bool> lazyOkCondition,
string strOkCondition = ""
)