3 ms·
Two more levels of blogs down, the actual proposal.[1] Definition: // Print has a type parameter T and has a single (non-type) // parameter s which is
by Animats 6y ago
Two more levels of blogs down, the actual proposal.[1] Definition:
// Print has a type parameter T and has a single (non-type)
// parameter s which is a slice of that type parameter.
func Print[T any](s []T) { ... }
Call:
Print[int]([]int{1, 2, 3})
Above, "any" is really just a synonym for "interface{}". You can have more restrictive type constraints on parameterized types by specifying other Go interfaces. This is vaguely similar to how Rust does it, and quite different from the C++ approach.
"This design does not support template metaprogramming or any other form of compile time programming."
[1] https://go.googlesource.com/proposal/+/refs/heads/master/design/go2draft-type-parameters.md https://go.googlesource.com/proposal/+/refs/heads/master/des...
- fanf2 6y agoMy understanding from the “Featherweight Go” paper https://arxiv.org/abs/2005.11710 https://arxiv.org/abs/2005.11710 is that generic types will not simply be a synonym for interface{} because the compiler will be able to monomorphize them - they do not require dynamic dispatch like interfaces.
- oconnor663 6y agoI think the comment above meant that the `any` keyword specifically is a synonym for `interface{}`, not that all generic types will be.
- mseepgood 6y ago> My understanding from the “Featherweight Go” paper https://arxiv.org/abs/2005.11710 https://arxiv.org/abs/2005.11710 is that generic types will not simply be a synonym for interface{} The `any` constraint is a synonym for the `interface{}` constraint.
- lights0123 6y ago(also C++ concepts)