errors/wrap.go looks like it could be made into a more general monad package?

81 views
Skip to first unread message

Gert

unread,
Jul 2, 2019, 9:03:20 PM7/2/19
to golang-nuts
https://github.com/golang/go/blob/master/src/errors/wrap.go

What if we replaced all error types in that file with a empty interface or a general monad interface? Can we make it work for other types too and introduce a new monad package instead a new error package only?

Gert

unread,
Jul 2, 2019, 9:40:36 PM7/2/19
to golang-nuts
On Wednesday, July 3, 2019 at 3:03:20 AM UTC+2, Gert wrote:
https://github.com/golang/go/blob/master/src/errors/wrap.go

What if we replaced all error types in that file with a empty interface or a general monad interface? Can we make it work for other types too and introduce a new monad package instead a new error package only?


package monad

type Monad interface {
Unwrap(m monad) monad
Is(m, target monad) bool
As(m monad, target interface{}) bool
}
 

Gert

unread,
Jul 2, 2019, 10:19:38 PM7/2/19
to golang-nuts
On Wednesday, July 3, 2019 at 3:03:20 AM UTC+2, Gert wrote:
https://github.com/golang/go/blob/master/src/errors/wrap.go

What if we replaced all error types in that file with a empty interface or a general monad interface? Can we make it work for other types too and introduce a new monad package instead a new error package only?


package monad

type Monad interface {
Unwrap() Monad

Gert

unread,
Jul 2, 2019, 10:52:38 PM7/2/19
to golang-nuts
On Wednesday, July 3, 2019 at 3:03:20 AM UTC+2, Gert wrote:
https://github.com/golang/go/blob/master/src/errors/wrap.go

What if we replaced all error types in that file with a empty interface or a general monad interface? Can we make it work for other types too and introduce a new monad package instead a new error package only?


```

package monad

type Monad interface {
Unwrap() Monad
}

func Unwrap(m Monad) Monad {
if m != nil {
return m.Unwrap()
}
return nil
}
```

```
package monad_test

import (
"monad"
"testing"
)

type monadT struct{ s string }

func (m monadT) Unwrap() monad.Monad { return nil }

type wrappedT struct {
s string
m monad.Monad
}

func (w wrappedT) Unwrap() monad.Monad { return w.m }

func TestUnwrap(t *testing.T) {
m1 := monadT{"monad"}
m2 := wrappedT{"monad wrap", m1}

testCases := []struct {
m    monad.Monad
want monad.Monad
}{
{nil, nil},
{wrappedT{"wrapped", nil}, nil},
{m1, nil},
{m2, m1},
{wrappedT{"wrap 3", m2}, m2},
}
for _, tc := range testCases {
if got := monad.Unwrap(tc.m); got != tc.want {
t.Errorf("Unwrap(%v) = %v, want %v", tc.m, got, tc.want)
}
}
}
```

Michal Strba

unread,
Jul 2, 2019, 11:28:25 PM7/2/19
to Gert, golang-nuts
First of all, this is not at all what "monad" means. A monad is a way to express threading of values between computations. A result of one computation gets threaded as an input to the next one, and so on.

Second, what would be the use-case of this other than errors?

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/92fe566f-3f18-43a2-91a6-4db21a1ef4ee%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Gert

unread,
Jul 3, 2019, 12:16:13 AM7/3/19
to golang-nuts
Fine I can change the name to wrapper no problem, but I do think there are cases where you have general types like for example xml and json and wrapped in types like html and geojson. I just think we going to be to late to realise that the new Unwrap Is As %w is very useful for other types too. Like we where to late with the io.File vs io.Writer debate because we simple didn't got enough experience with it.


On Wednesday, July 3, 2019 at 5:28:25 AM UTC+2, Michal Strba wrote:
First of all, this is not at all what "monad" means. A monad is a way to express threading of values between computations. A result of one computation gets threaded as an input to the next one, and so on.

Second, what would be the use-case of this other than errors?

To unsubscribe from this group and stop receiving emails from it, send an email to golan...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages