Skip to content

Have a function to convert boolean to Option<()> in std #2227

Description

Taking inspiration from haskell (http://hackage.haskell.org/package/base-4.10.0.0/docs/Control-Monad.html#v:guard), we could have a guard : fn(bool) -> Option<()> function in std

This lets us write more pleasant code. For example instead of

let x = do catch {
    if is_admin(user) {
        calculate_important_stuff()
    } else {
        None
    }
};

we can do this

let x = do catch {
    guard(is_admin(user))?;
    calculate_important_stuff()
};

basic implementation:

fn guard(b: bool) -> Option<()> {
    if b { Some(()) } else { None }
}

Or maybe even have a Try trait implementation for bool? Not sure if that's beautiful though

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    T-libs-api[DEPRECATED; DO NOT USE]

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions