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

proposal: Go 2: language: make slices of constant strings constant if the indexes are constant #28591

Open
ainar-g opened this issue Nov 4, 2018 · 2 comments

Comments

Projects
None yet
4 participants
@ainar-g
Copy link
Contributor

commented Nov 4, 2018

This will probably be assigned a Go2 label, although I think that it could be done during Go 1.

Consider multiline string constants:

const multiline = `
This is a long text
that spans multiple lines
and then ends.
`

If you feed this text to a parser and it finds a problem on the first line ("This is a long text"), it will actually report that the error is on the second line because of the leading newline. One might think that this is easily solved by slicing the string:

const multiline = `
This is a long text
that spans multiple lines
and then ends.
`[1:]

But this won't compile. The Spec currently says:

(…) For untyped string operands the result is a non-constant value of type string. (…)

Why not make it constant? After all, len(multiline) is constant.


There are other solutions to this. One could just use multiline[1:] everywhere where one doesn't want the leading newline. Or start "This is a long text" on the first line, but that hurts readability and "editability". Another solution is to make multiline a variable. The use case is pretty obscure and there isn't an immediate need for this, so this proposal is more about a question, why are slices (and indexing!) of constant strings not constant in the first place?

@gopherbot gopherbot added this to the Proposal milestone Nov 4, 2018

@gopherbot gopherbot added the Proposal label Nov 4, 2018

@mvdan mvdan changed the title proposal: make slices of constant strings constant if the indexes are constant proposal: spec: make slices of constant strings constant if the indexes are constant Nov 5, 2018

@ianlancetaylor ianlancetaylor changed the title proposal: spec: make slices of constant strings constant if the indexes are constant proposal: Go 2: language: make slices of constant strings constant if the indexes are constant Nov 5, 2018

@ianlancetaylor ianlancetaylor added the Go2 label Nov 5, 2018

@ianlancetaylor

This comment has been minimized.

Copy link
Contributor

commented Nov 5, 2018

Marking as Go 2 because we've decided that all language changes are Go 2.

I know that this has been discussed in the past but I don't remember why it wasn't changed. Maybe simply because it rarely comes up in practice.

@ianlancetaylor

This comment has been minimized.

Copy link
Contributor

commented Dec 11, 2018

We should also consider accepting

const c = "abc"[1]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.