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
package main
import (
"fmt"
"math"
)
type Float interface {
~float32 | ~float64
}
func NaN[T Float]() T {
return T(math.NaN())
}
func main() {
var n float32
n = NaN()
fmt.Println(n)
}
Run
go run main.go
What did you expect to see?
NaN
What did you see instead?
./main.go:18:9: cannot infer T
In order for the code to work, you have to provide the type of the variable n:
n = NaN[float32]()
Since the function NaN returns a value of type T and the the type of variable n is known to be float32, the compiler should that T should be float32, but it does not.
The text was updated successfully, but these errors were encountered:
Thanks for the report. That is correct. We only infer type arguments from function arguments or from other type arguments, not from the context in which a function occurs. We aren't going to be adding new kinds of type inference until we have more experience with the forms of inference we already have, so closing. We can reexamine this idea later.
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
Yes.
What operating system and processor architecture are you using (
go env
)?go env
OutputWhat did you do?
main.go
Run
What did you expect to see?
What did you see instead?
In order for the code to work, you have to provide the type of the variable n:
Since the function NaN returns a value of type T and the the type of variable n is known to be float32, the compiler should that T should be float32, but it does not.
The text was updated successfully, but these errors were encountered: