samber / mo

🦄 Monads and popular FP abstractions, powered by Go 1.18+ Generics (Option, Result, Either...)
https://pkg.go.dev/github.com/samber/mo
MIT License
2.47k stars 80 forks source link

Result.Map expects new result to be of the same type #31

Closed Yakiyo closed 10 months ago

Yakiyo commented 11 months ago

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

Yakiyo commented 11 months ago

I just saw that apparantly struct methods can't have generic types when the struct itself has generics on it. So it feels like this isn't possible?