Description
Author background
- Would you consider yourself a novice, intermediate, or experienced Go programmer?
Experienced.
- What other languages do you have experience with?
Ruby, JavaScript, Dart, Kotlin, Java, C, Python, and several others to varying degrees.
Related proposals
- Has this idea, or one like it, been proposed before?
Probably, considering that type inference has been kept simple purposefully but with an open door to look into it again after generics are out. Generics are out, so...
- Does this affect error handling?
Not directly, no.
- Is this about generics?
Yes.
Proposal
- What is the proposed change?
Currently, the following fails to compile:
type Simpler[T any] interface {
Simple() T
}
type Complexer[T any] interface {
Simpler[T]
Complex(T)
}
func DoSomething[T any](s Simpler[T]) {}
func main() {
var c Complexer[int]
DoSomething(c) // Fails here. Works correctly if specified manually: DoSomething[int](c)
}
This makes dealing with complex type systems where there are overlapping interfaces with optional functionality somewhat annoying.
As an example of this issue coming up in real code, I have a package for state tracking that has a basic interface type that it exports and several types that extend it. Dealing with the extension types is noticeably more awkward than just dealing with the base type because of the lack of inference. For example,
type State[T any] interface {
Listen(func(T))
}
type MutableState[T any] interface {
State[T]
Set(T)
}
// Get is gets the current value of a state, but the type inference fails if
// dealing with a MutableState[T]. It works as expected when dealing
// with just a State[T] directly, however.
func Get[T any](s State[T]) T { ... }
- Who does this proposal help, and why?
Anyone dealing with generics and basic interfaces and more complicated ones that build on top of those.
Costs
- Would this change make Go easier or harder to learn, and why?
Easier, I think. Fewer rules about how and where type inference works and doesn't work is simpler and easier to deal with.
- What is the cost of this proposal? (Every language change has a cost).
Unsure. I am not an expert on type systems, but I would guess that this would complicate the implementation somewhat, possibly resulting in compiler slowdown, at least in cases where this specific form of inference is used.
- How many tools (such as vet, gopls, gofmt, goimports, etc.) would be affected?
None, I think. Just the compiler.
- What is the compile time cost?
Possible increase in specific situations, but probably not terrible. I think.
- What is the run time cost?
None.
- Can you describe a possible implementation?
No.
- Do you have a prototype? (This is not required.)
No.