kdungs / go-result

Result types for Go; because (T, error) can be considered a ✨ monad ✨.
MIT License
1 stars 0 forks source link

Investigate how to forward as an interface #9

Closed kdungs closed 1 year ago

kdungs commented 1 year ago

E.g.

f := result.Wrap(os.Open("foo"))
result.MapE(io.ReadAll)

doesn't compile because in MapE we have B = *os.File which implements io.Reader but is not identical to it...

kdungs commented 1 year ago

Doesn't work because among other things https://go.dev/doc/faq#covariant_types.

//edit: The reason I still think this is more of a generics issue than a covariant types issue is that the following works:

func c[A any, B interface{ io.Reader }](f func(A) B, g func(io.Reader) string) func(A) string {
    return func(a A) string {
        return g(f(a))
    }
}

but the rules for constraints currently prohibit using a type parameter inside interface {}.

kdungs commented 1 year ago

See also https://github.com/golang/go/issues/58650

kdungs commented 1 year ago

Collected examples from the discussion on the Gopher slack...