Description
I was recently trying roughly the following segment of code expecting it to work:
https://go.dev/play/p/yTVhxJ82pMR
Lines 47 and 48 are commented out; lines 53 and 54 are what I would have to do if I want to use First
. If possible, it'd be great to not have to fully specify the types and instead rely entirely on type inference.
Speaking as a complete newbie / uninformed person here, I'd think the algorithm would be:
For slices,
- Input S matches left side of union ~[]V
- V is inferred to be the value type of the input slice
- K is not required and is ignored
For maps,
- Input S matches right side of union ~map[K]V
- K is inferred to be the key type of the input map
- V is inferred to be the value type of the input map
I recognize this is a naive and simple proposal that absolutely does not capture the underlying complexity. Go also currently requires all types to be specified, and in the slice case, there is no K
; this requirement would need to be relaxed in the type union case.
The closest related issue I see is #56975, but that is different because the proposal is specific to functions and is actually not about a union.