What is the point of a String() contract in generics?

Posted on

I watched the talk that is linked at this url: https://blog.golang.org/why-generics. It’s a pretty good talk that I would recommend for anyone coming from a language like python which is dynamically typed.

All of the examples make sense to me, except for this one:

contract Stringer(T) {
    T String() string

What benefit does this give us over just passing a type that implements fmt.Stringer? I’m specifically referencing this explanation here which I don’t understand and I think the speaker glossed over a little too quickly:

You may notice that this contract looks like the fmt.Stringer interface, so it’s worth pointing out that the argument of the ToStrings function is not a slice of fmt.Stringer. It’s a slice of some element type, where the element type implements fmt.Stringer. The memory representation of a slice of the element type and a slice of fmt.Stringer are normally different, and Go does not support direct conversions between them. So this is worth writing, even though fmt.Stringer exists.