Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

spec: spec is incorrect about the requirement for parentheses around the type in a conversion #4109

Closed
griesemer opened this issue Sep 19, 2012 · 3 comments

Comments

@griesemer
Copy link
Contributor

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.
@griesemer
Copy link
Contributor Author

Comment 1:

Labels changed: added documentation, languagechange.

@rsc
Copy link
Contributor

rsc commented Sep 20, 2012

Comment 2:

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

@griesemer
Copy link
Contributor Author

Comment 3:

This issue was closed by revision 3188ffc.

Status changed to Fixed.

@griesemer griesemer self-assigned this Oct 3, 2012
@golang golang locked and limited conversation to collaborators Jun 24, 2016
This issue was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants