google / google/go-cmp

Comparing contravariant types doesn't work as expected using Transformer

Open
#261 5 comments 0 reactions 0 assignees View on GitHub
question
Dominant language
Go
Stars
4.7k
Forks
243
PR merge metrics
No merged PRs in 30d

Description

```
package main

import (
"fmt"

"github.com/google/go-cmp/cmp"
)

type intHolder struct {
i int
}

func main() {
got := []intHolder{
{i: 1},
}
want := []int{1}
fmt.Println(cmp.Diff(want, got, cmp.Transformer("myTransformer", func(h intHolder) int {
return h.i
})))
}
```

produces:

```
interface{}(
- []int{1},
+ []main.intHolder{{i: 1}},
)
```

https://play.golang.org/p/GQqAZmbYUD8

Digging into the implementation revealed effectively that Transform is _skipped_ whenever the types being compared aren't identical.

The same thing happens when `got` and `want` are scalars rather than slices: https://play.golang.org/p/r2O2UKYRD-r

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.