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
The spec says ( http://tip.golang.org/ref/spec#Conversions ):
"If the type (in a conversion) starts with an operator it must be
parenthesized"
This is indeed the case for:
(*T)(x) otherwise it would be *(T(x))
(<-chan T)(x) otherwise it would be <-(chan T(x))
but it is not required for:
[n]T(a) same as ([n]T)(a)
[]T(a) same as ([]T)(a)
both of which start with an operator. All, gc, gccgo, and gofmt accept this syntax and
we cannot easily blame them for being incorrect as there is plenty of code in the
library that converts strings into []bytes as in: []byte("foo") (try e.g.,
http://tip.golang.org/search?q=%5C%5B%5C%5Dbyte%5C%28.*%5C%29 ).
Furthermore, conversions to literal function types without result specification require
parentheses:
(func())(f) otherwise it might be (func())(f) or (func()(f))x or func()(f){...} but we don't know until we see the x or {...}
even though "func" is not an operator but a keyword. All, gc, gccgo, and gofmt
require parenthesis in this case. To make things more complicated, conversions to
function literal types specifying a result (even if empty) do not require parentheses:
func()(T)(f) same as (func()(T))(f)
func()T(f) same as (func()T)(f)
func()())(f) same as (func())(f)
Proposal: The spec should be adjusted to say:
"If the type (in a conversion) starts with "*", "<-", or
"func" it must be parenthesized"
This would be a backward-incompatible change. We need to back it up by confirming that
such function conversions are indeed rare or do not occur in the wild.
The text was updated successfully, but these errors were encountered:
I interpreted the word operator as meaning a Go expression operator,
so that excludes [n] and []. In practice the only overlap then is <-
and *. Cleaning up that bit in the spec should be easy.
As for func: yuck. Probably we should say all func conversion types
must be parenthesized, but gofmt should continue to accept the
unparenthesized syntax where it does now and insert the parens.
Russ
The text was updated successfully, but these errors were encountered: