• nickA
    link
    fedilink
    arrow-up
    13
    arrow-down
    1
    ·
    edit-2
    1 year ago

    Having a Result[T, Err] monad that could represent either the data from a successful operation or an error. This can be generalised to the Either[A, B] monad too.

      • oessessnex@programming.dev
        link
        fedilink
        arrow-up
        11
        ·
        edit-2
        1 year ago

        Nope. Monads enable you to redefine how statements work.

        Let’s say you have a program and use an Error[T] data type which can either be Ok {Value: T} or Error:

        var a = new Ok {Value = 1};
        var b = foo();
        return new Ok {Value = (a + b)};
        

        Each statement has the following form:

        var a = expr;
        rest
        

        You first evaluate the “expr” part and bind/store the result in variable a, and evaluate the “rest” of the program.

        You could represent the same thing using an anonymous function you evaluate right away:

        (a => rest)(expr);
        

        In a normal statement you just pass the result of “expr” to the function directly. The monad allows you to redefine that part.

        You instead write:

        bind((a => rest), expr);
        

        Here “bind” redefines how the result of expr is passed to the anonymous function.

        If you implement bind as:

        B bind(Func[A, B] f, A result_expr) {
           return f(result_expr);
        }
        

        Then you get normal statements.

        If you implement bind as:

        Error[B] bind(Func[A, Error[B]] f, Error[A] result_expr) {
           switch (result_expr) {
               case Ok { Value: var a}:
                   return f(a);
               case Error:
                   return Error;
           }
        }
        

        You get statements with error handling.

        So in an above example if the result of foo() is Error, the result of the statement is Error and the rest of the program is not evaluated. Otherwise, if the result of foo() is Ok {Value = 3}, you pass 3 to the rest of the program and you get a final result Ok {Value = 4}.

        So the whole idea is that you hide the if Error part by redefining how the statements are interpreted.

      • barsoap@lemm.ee
        link
        fedilink
        arrow-up
        4
        ·
        edit-2
        1 year ago

        “Some generic class” with specific methods and laws, Monads are an algebraic structure and you want those laws included same as if you enable some type to use + you want to have a 0 somewhere and x + 0 == x to hold. Like "foo" + "" == "foo" in the case of strings, just as an example.

        In Rust, Result and Option actually are monads. Let’s take Option as example:

        • pure x is Some(x)
        • a >>= b is a.and_then(b)

        Then we have:

        • Left identity: Some(x).and_then(f)f(x)
        • Right identity: x.and_then(Some)x
        • Associativity: m.and_then(g).and_then(h)m.and_then(|x| g(x).and_then(h))

        Why those laws? Because following them avoids surprises like x + 0 /= x.

        Rust’s type system isn’t powerful enough to have a Monad trait (lack of HKTs) hence why you can’t write code that works with any type that implements that kind of interface. Result names >>= and_then, just like Option does so the code reads the same but you’ll have to choose between Option or Result in the type signature, the code can’t be properly generic over it.