Hacker News new | ask | show | jobs
by Animats 1990 days 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/des...

2 comments

My understanding from the “Featherweight Go” paper 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.
> My understanding from the “Featherweight Go” paper 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.

I think the comment above meant that the `any` keyword specifically is a synonym for `interface{}`, not that all generic types will be.
(also C++ concepts)