Hi, I don’t know the relationship of typeparams to type switches, and typeparams as thing for fast code.
I’m looking for the Go equivalent to C++ function overloading and C++ resolving the types at compile-time.
May I post a C++ example that I want to implement in Go?
foo(int a) { /* do int things */ }
foo(float a) { /* do float things */ }
So, if I want this example from above in Go, with type checking at runtime (slow, unwanted), I can write:
func foo(a interface{}) {
switch v := i.(type) {
case int:
case float32:
default:
}
}
That is old Go, and now in new Go I can also write (fast) code like this?
func foo[T any](a T) {
switch v := a.(type) {
case int:
case float32:
default:
}
}
This looks like there would be type checking at runtime (unwanted), and maybe the type switch will not compile. Should have checked it myself but I need to learn generic Go in general, so I’m asking.
I have not read the right design document in the Go documentation. I’m not sure what the Go team tried to solve and what not. I tried to use any function overloading that might have been in Go and the compiler told me I’m redeclaring functions and this is not how it goes.