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: add a interval notation to specify elements in the result of a slice expression #41924

Closed
ghost opened this issue Oct 12, 2020 · 14 comments
Closed
Assignees
Milestone

Comments

@ghost
Copy link

@ghost ghost commented Oct 12, 2020

s := []int{1, 2, 3, 4, 5}


a := s[1:4]	// len == 3, contains {2, 3, 4}
b := s[1:4]	// len == 3, contains {3, 4, 5}

spec doesn't precisely specify which elements will be in the resulting slice.

a interval notation can be added to the spec.

@gopherbot gopherbot added this to the Proposal milestone Oct 12, 2020
@gopherbot gopherbot added the Proposal label Oct 12, 2020
@randall77
Copy link
Contributor

@randall77 randall77 commented Oct 12, 2020

b := s[1:4]	// len == 3, contains {3, 4, 5}

No. b is the same as a here. It contains {2, 3, 4}.

Not sure what you are intending. But if something is unclear in the spec, please be clearer about what it is that is confusing. You're proposing a fix and I don't understand what the problem is.

@ghost
Copy link
Author

@ghost ghost commented Oct 12, 2020

yes, here b is same as a. i wrote them as example.

i am asking for a sentence in the spec that specify the elements using a interval notation.

@davecheney
Copy link
Contributor

@davecheney davecheney commented Oct 12, 2020

@cubte would you please review https://golang.org/ref/spec#Slice_expressions and explain how you would like to see it improved.

@davecheney davecheney added WaitingForInfo and removed Proposal labels Oct 12, 2020
@ghost
Copy link
Author

@ghost ghost commented Oct 12, 2020

The spec does not specify which elements get included in the resulting slice using words. It is demonstrated using example code.

@davecheney
Copy link
Contributor

@davecheney davecheney commented Oct 12, 2020

The indices low and high select which elements of operand a appear in the result.

Please remember that a subslice is not a copy of the contents of the source slice, it creates a new slice header value suitably adjusted.

@ghost
Copy link
Author

@ghost ghost commented Oct 12, 2020

[low, high) and (low, high] have the same length.

@davecheney
Copy link
Contributor

@davecheney davecheney commented Oct 12, 2020

True, but the subslice is [low:high) not (low, high]. Are you saying that you would like to see that state explicitly in the spec?

@ghost
Copy link
Author

@ghost ghost commented Oct 12, 2020

Are you saying that you would like to see that state explicitly in the spec?

Yes.

@davecheney
Copy link
Contributor

@davecheney davecheney commented Oct 12, 2020

Thank you for explaining.

/cc @ianlancetaylor

@randall77
Copy link
Contributor

@randall77 randall77 commented Oct 12, 2020

It seems clear to me:

a := [5]int{1, 2, 3, 4, 5}
s := a[1:4]

the slice s has type []int, length 3, capacity 4, and elements

s[0] == 2
s[1] == 3
s[2] == 4

I don't see any possibility for confusion here. It shows exactly the entries in the resulting slice. You can map them back to the input array easily enough.

@ianlancetaylor ianlancetaylor changed the title proposal: spec: Add a interval notation to specify elements in the result of a slice expression spec: add a interval notation to specify elements in the result of a slice expression Oct 12, 2020
@ianlancetaylor ianlancetaylor modified the milestones: Proposal, Unplanned Oct 12, 2020
@griesemer
Copy link
Contributor

@griesemer griesemer commented Oct 13, 2020

Note that the prose says explicitly: "The indices low and high select which elements of operand a appear in the result."

The reason for the example's choice of element values 1, 2, 3, 4, 5 is exactly so we don't need to introduce another formalism but can illustrate the behavior with a simple example in case it shouldn't be already clear from the prose.

Happy to revisit if you have a concrete suggestion as to how to make this clearer without making it more complicated. Closing for now. Thanks.

@griesemer griesemer closed this Oct 13, 2020
@ghost
Copy link
Author

@ghost ghost commented Oct 13, 2020

Note that the prose says explicitly: "The indices low and high select which elements of operand a appear in the result."

But it does not say exactly which elements gets selected.

The reason for the example's choice of element values 1, 2, 3, 4, 5 is exactly so we don't need to introduce another formalism but can illustrate the behavior with a simple example in case it shouldn't be already clear from the prose.

I think that sentence doesn't make it fully clear. Instead it requires to explicitly rely on the example.

An interval notation specifying the indices of elements get selected from operand can make it much better to understand. Then the example will be an example.

@griesemer
Copy link
Contributor

@griesemer griesemer commented Oct 13, 2020

It may not be "fully clear" in a mathematical sense, but it is sufficiently obvious what is meant that for 12+ years nobody objected.

As I said, I am not against improving this if you send us a concrete suggestion that is clearly better and just as easy to read. Thanks.

@ghost
Copy link
Author

@ghost ghost commented Oct 19, 2020

for 12+ years nobody objected.

Maybe everyone here is old and took it for granted. But i am 19 years old and i can't.

I am not a native English speaker. Constructing such sentence is relatively hard for me. I wish you will understand me and improve this.

The best i have tried is,

Elements from index low to high - 1 of operand a appear in result.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
5 participants
You can’t perform that action at this time.