Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
}
add(2, 4) //returns int(6)
add("Hello ", "World") //returns string("Hello World")[
add(2, "World") //compiler error: cannot use 2, "World" (type int, string) in arguments to add
add(struct{}{}, [2]int{}) //compiler error: cannot use struct{}{}, [2]int{} (type struct{}, [2]int) in arguments to add
//New types can be created based on the generic type.
type GenericSlice generic
//Function that creates a new slice of the provided argument with length, can be inlined by the compiler.
//The return value is converted to a GenericSlice.
func MakeSliceOf(a generic, length int) GenericSlice {
//The builtin 'typeof' function is replaced by the type of it's argument.
return GenericSlice(make([]typeof(a), length))
}
//The get method operates on explicit GenericSlice types.
//The return value is automatically inferred based on the GenericSlice, can be inlined by the compiler.
func (g GenericSlice) Get(index int) generic {
return g[index]
}
//The set method operates on explicit GenericSlice types.
//The value argument must be the subtype of the slice or a compiler error is raised.
func (g GenericSlice) Set(index int, value generic) {
g[index] = value
}
var slice = MakeSliceOf(int(0), 10)
slice.Set(0, 22)
fmt.Println(slice.Get(0)) //22
slice.Set(0, "Hello World") //compiler error: cannot use "Hello World" (type string) in arguments to slice.Set
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment