Skip to content

Instantly share code, notes, and snippets.

@faiface
faiface / .md
Last active January 23, 2020 04:53
Go 2 generics counterproposal: giving up restricting types

Go 2 generics counterproposal: giving up restricting types

"I want to make a generic function which works on any type that satisfies these and these constraints."

When we think about generics, this is the sort of problem that pops up into our minds. The critical part is restricting the set of types our function intends to work on. Trying to solve this problem of restriction has led people to what I call reasons why we hesitate to have generics in Go.

C++ templates with horrific error messages, or even it's new concepts, Java's T extends Comparable<T>, Rust's generic traits, Haskell's type classes, and sadly even contracts from the [original generics proposal by the Go Team](https://go.googlesource.com/proposal/+/master/desig

@Splizard
Splizard / Go2GenericType.go
Last active June 10, 2020 16:53
Go 2 generics without syntax changes, 1 new type, 1 new builtin.
//A generics proposal for Go, introducing one new type (generic) and one new builtin function (typeof), no syntax changes.
//Function add takes arguments of any type.
//The return value is automatically inferred based on the arguments, can be inlined by the compiler.
//Both arguments must be the same type and addable or else a compiler error will be raised.
//It's the developer's responsibility to document which types can be passed to this function.
func add(a, b generic) generic {
return a+b
}