Grokking Advanced Go
Vote

0% completed

​

Type Inference

The function below doubles a price or a weight. Which of the five uses in main compile, given that none of them writes a type argument?

func Double[T ~int64 | ~float64](v T) T { return v * 2 } func main() { var price int64 = 2500 _ = Double(price) _ = Double(1.5) _ = Double(3) var f = Double var g func(int64) int64 = Double _, _ = f, g }
./main.go:10:12: int does not satisfy ~int64 | ~float64 (int missing in ~int64 | ~float64) ./main.go:11:10: cannot use generic function Double without instantiation

.....

.....

.....

Like the course? Get enrolled and start learning!

Reading Progress

0%


Vote for new content