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)
}
}
}
```

-- 
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+unsubscr...@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.

Reply via email to