4 ms·
Can someone tell me what is going on here: the release notes [https://golang.org/doc/go1.10#language https://golang.org/doc/go1.10#language] state that > A cor
by simonbyrne 9y ago
Can someone tell me what is going on here: the release notes [https://golang.org/doc/go1.10#language https://golang.org/doc/go1.10#language] state that
> A corner case involving shifts by untyped constants has been clarified, and as a result the compilers have been updated to allow the index expression `x[1.0 << s]` where s is an untyped constant; the go/types package already did.
But the docs [https://golang.org/ref/spec#Operators https://golang.org/ref/spec#Operators] state that
> `var u = 1.0<<s // illegal: 1.0 has type float64, cannot shift`
- cshenton 9y agoIt's probably to do with the fact that one is a "number". If I'm correct, that means in `x[1.0 << s]`. 1.0 is a "number", therefore the shift is valid, but in `var u = 1.0 << s`, it's a float64, and therefore the shift is invalid. see also: https://blog.golang.org/constants https://blog.golang.org/constants
- simonbyrne 9y agoAh, thanks.
- tapirl 9y ago@cshenton 's answer is not perfect. The "var u = 1.0<<s" in go spec is illegal is because s is a variable, so 1.0 will be deduced as a flaot64. However, in "x[1.0 << s]", s is a constant, so 1.0 will be deduced as int.