Gophers' superpower is required and highly appreciated.
I have two types, A and B, each of which has a method named "M". This method accepts a string and returns an instance of the type it is declared on.
```go
type A struct {}
func (a A) M (string) A {
return a
}
type B struct {}
func (b B) M (string) B {
return b
}
```
What's the best way to declare a generic function that receives an instance of one of these types along with a string? Depending on the string, this function should either return the instance it received or call method M on it and return the instance produced by this method.
Currently, I've come up with two solutions, but I'm not entirely satisfied with either:
https://go.dev/play/p/h5jzISRyTkF In this approach, I'm not fond of how I must call the f function on line 30. Without specifying a type parameter, the call fails to compile. Additionally, I'm concerned about potential panics on line 23. If I expand the code with new types in the future, there's a risk of type assertion failures.
https://go.dev/play/p/rHHjV_A-hvK While this solution appears cleaner, it's prone to panicking if I introduce another type with an incompatible M signature (or without M method completely)
Maybe someone has better ideas.
Thank you.