I only recently saw this library and it opened the doors to a lot of new stuff for me, i specially liked that it has types like Result and Option which are great for me, since i came from Rust. But what felt weird to me was the type of Result.Map
For a Result, the Map function returns a new Result, this allows me to modify the internal value but im restricted to it being the same type.
On the otherhand in case of rust's Result, Result.Map converts a result of type Result<T, E> to Result<U, E>. I can convert the inner Ok value to a diff type, it can be either the same type or diff. while for mo's case, im bound to having the same type. The following exam fails in mo's case
package main
import (
"fmt"
"github.com/samber/mo"
)
func main() {
ok := mo.Ok([]string{"b"})
r := ok.Map(func(value []string) (string, error) {
return value[0], nil
})
fmt.Println(r.MustGet())
}
While i could just do ok.MustGet()[0] in this , this is usually not always the case when i'm returning a Result type from a custom function. It would be helpful if the Map function could convert type T to a new type U. Otherwise we explicitly need to unwrap the result, convert it and rewrap it in a result
I only recently saw this library and it opened the doors to a lot of new stuff for me, i specially liked that it has types like Result and Option which are great for me, since i came from Rust. But what felt weird to me was the type of Result.Map For a Result, the , this allows me to modify the internal value but im restricted to it being the same type.
On the otherhand in case of rust's Result, Result.Map converts a result of type
Map
function returns a new ResultResult<T, E>
toResult<U, E>
. I can convert the inner Ok value to a diff type, it can be either the same type or diff. while for mo's case, im bound to having the same type. The following exam fails in mo's caseWhile i could just do
ok.MustGet()[0]
in this , this is usually not always the case when i'm returning a Result type from a custom function. It would be helpful if the Map function could convert type T to a new type U. Otherwise we explicitly need to unwrap the result, convert it and rewrap it in a result