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.61k stars 85 forks source link

Irritating lack of type inference on Option for None and Result for Err #29

Closed effinsky closed 1 year ago

effinsky commented 1 year ago

I realize this is a limitation of the compiler's type inference mechanism but it looks bad to have to explicitly state a type parameter for the None and Err variants. Can anything be done to avoid this and clean up the use of Option and Result?

Here's what I mean for Result, for instance:

Screenshot 2023-08-15 at 11 56 56

Here's what would be cooler:

Screenshot 2023-08-15 at 11 56 42

Ok, and here's what would be really nice, with options for wrapping this:

Screenshot 2023-08-15 at 11 56 04
samber commented 1 year ago

I suppose we can add a mo.Errf() helper, but your first feedback is due to compiler limitation. Go is not able to infer Err generic type from the output generic type (here Result[string]).

effinsky commented 1 year ago

I know -- it hurts!

samber commented 1 year ago

Added to v1.10.0