-
Notifications
You must be signed in to change notification settings - Fork 18k
cmd/compile: unable to use composite literal directly as part of range expression #69017
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
Comments
Per https://go.dev/ref/spec#Composite_literals. This is working as intended. Parsing the brackets as a composite literal would require either type information in the parser or unbounded lookahead, both of which are properties Go has historically sought to avoid. |
Use either Similar case: |
As @zephyrtronium and @zigo101 mentioned, this works as intended, and has a simple workaround. Thanks. |
Got it, thanks all! |
Go version
go version go1.23.0 linux/amd64
Output of
go env
in your module/workspace:What did you do?
https://go.dev/play/p/t_G0ZOb51EG
The statement
for _, v := range S{}.Slice {}
throws a syntax error due to the direct use of composite literalS{}
as part of the range expression.Will be good to see composite literal being supported directly inside range expression, especially with the introduction of range-over-func (e.g.
range interval{a, b}.Step()
).As a workaround, the statement
for _, v := range (S{}).Slice {}
works but appears unintuitive, when expressionS{}.Slice
can be trivially used in most other statements.What did you see happen?
syntax error: unexpected . at end of statement
on the line where the composite literal is used as part of the range expression.What did you expect to see?
No syntax error.
The text was updated successfully, but these errors were encountered: