Generics at Home Go Code
Me: can we get generics? Pops: no, we have generics at home generics at home: type Comparer interface { Compare(i interface{}) int } // This function panics if i is not a Dog. func (d Dog) Compare(i interface{}) int { if dog, ok := i.(Dog); ok { return d.Weight-dog.Weight } else { panic(fmt.Errorf( "i is not a dog (%s=%#v)", "i", i, )) } }
A programming meme contrasts a request for generics with a homemade substitute in Go. The code defines a Comparer interface that only works with Dog values and panics when given anything else, mocking an awkward workaround that technically resembles generics but is not truly generic.
- @profunctor_io / 2701 2019-07-23
- Type
- image
- Size
- 514×487 · 40.9 KiB
- Mood
- sarcastic
- Language
- en
- Added