ResultExtensionsOkIf Method

Overload List

OkIf(Boolean, String) Creates a result as Ok variant if the okCondition holds. Otherwise, it will map into an Err.
C#
static Res ValidateInput(Form form)
{
    return OkIf(!form.HasEmptyFields())
        .OkIf(form.Date <= DateTime.Now)
        // chained validation calls
        .OkIf(repo.AlreadyContains(form.Id));
}
OkIfT(Boolean, T, String) Creates a result of T as Ok variant with value value if the okCondition holds. Otherwise, it will map into an Err.
C#
Shape shape = GetShape(); // valid only if shape has a positive base area.
Res<Shape> resultShape = OkIf(shape.GetBaseArea() > 0, shape);
OkIfT(Boolean, FuncT, String) Creates a result of T as Ok variant with value lazyGetValue() if the okCondition holds. Otherwise, it will map into an Err. Note that the lazyGetValue is only evaluated if the okCondition holds.
C#
Res<User> user = TryGetUser();
// create a database connection (expensive) only if the user IsOk.
Res<Conn> conn = OkIf(user.IsOk, () => CreateDatabaseConnection());

See Also