You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In the sandbox code, the function ReturnsUnsetOptionalInteger should have compiled because the generic parameter V could be inferred from it's explicit return type Optional[int]
What did you see instead?
./prog.go:33:25: cannot infer V (prog.go:19:23)
The text was updated successfully, but these errors were encountered:
// You can edit this code!
// Click here and start typing.
package main
import "fmt"
type Optional[V any] struct {
Value V
Set bool
}
func NewSetOptional[V any](val V) Optional[V] {
return Optional[V]{
Value: val,
Set: true,
}
}
func NewUnsetOptional[V any]() Optional[V] {
return Optional[V]{
Set: false,
}
}
func ReturnsOptionalInteger() Optional[int] {
// THIS IS FINE:
return NewSetOptional(3)
}
func ReturnsUnsetOptionalInteger() Optional[int] {
// DOES NOT COMPILE:
// should be able to infer V from function return type
return NewUnsetOptional()
// Workaround:
// return NewUnsetOptional[int]()
}
func main() {
fmt.Println("%v", ReturnsOptionalInteger())
fmt.Println("%v", ReturnsUnsetOptionalInteger())
}
andrewortman
changed the title
generics: generic type parameter cannot be inferred from calling function's return typee
generics: generic type parameter cannot be inferred from calling function's return type
Dec 30, 2021
andrewortman
changed the title
generics: generic type parameter cannot be inferred from calling function's return type
go/types: generic type parameter cannot be inferred from calling function's return type
Dec 30, 2021
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
With the current beta1 release
What operating system and processor architecture are you using (
go env
)?go env
OutputWhat did you do?
Reproduction: https://go.dev/play/p/GYUfSmIuzx4?v=gotip
What did you expect to see?
In the sandbox code, the function
ReturnsUnsetOptionalInteger
should have compiled because the generic parameter V could be inferred from it's explicit return type Optional[int]What did you see instead?
The text was updated successfully, but these errors were encountered: