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

[Arrays] #4121

Open
dlangBugzillaToGithub opened this issue Dec 9, 2021 · 1 comment
Open

[Arrays] #4121

dlangBugzillaToGithub opened this issue Dec 9, 2021 · 1 comment

Comments

@dlangBugzillaToGithub
Copy link

Kurt Krueckeberg reported this on 2021-12-09T14:14:25Z

Transferred from https://issues.dlang.org/show_bug.cgi?id=22580

CC List

Description

I am new to D, but the code example explanation in section 12.6 is confusing. The code example in section 12.6 refers to the "slice operator":

"When the slice operator appears as the left-hand side of an assignment expression, it means that the contents of the array are the target of the assignment rather than a reference to the array..."

Is there really a special "slice operator"? The operator used in the example in section 12.6 is array index operator, [].  So shouldn't the explanation be changed to refer to the index operator (being applied to a slice that appears on the left-hand side of an assignment statement)? To me, it is clearer to say something like:

"When the slice appears on the left-hand side of an assignment with the index operator, it means that the contents of the array are the target of the assignment rather than a reference to the array..."

Or to say:

"When the slice is indexed with the [] operator and it appears on the left-hand side of an assignment, it means that the contents of the array are the target of the assignment rather than a reference to the array..."

Or simply:

"When the slice is indexed and it appears on the left-hand side of an assignment, it means that the contents of the array are the target of the assignment rather than a reference to the array..."
@dlangBugzillaToGithub
Copy link
Author

andrej.mitrovich (@AndrejMitrovic) commented on 2022-07-06T07:55:39Z

(In reply to Kurt Krueckeberg from comment #0)
> I am new to D, but the code example explanation in section 12.6 is
> confusing. The code example in section 12.6 refers to the "slice operator":
> 
> "When the slice operator appears as the left-hand side of an assignment
> expression, it means that the contents of the array are the target of the
> assignment rather than a reference to the array..."
> 
> Is there really a special "slice operator"? The operator used in the example
> in section 12.6 is array index operator, [].  So shouldn't the explanation
> be changed to refer to the index operator (being applied to a slice that
> appears on the left-hand side of an assignment statement)? To me, it is
> clearer to say something like:
> 
> "When the slice appears on the left-hand side of an assignment with the
> index operator, it means that the contents of the array are the target of
> the assignment rather than a reference to the array..."
> 
> Or to say:
> 
> "When the slice is indexed with the [] operator and it appears on the
> left-hand side of an assignment, it means that the contents of the array are
> the target of the assignment rather than a reference to the array..."
> 
> Or simply:
> 
> "When the slice is indexed and it appears on the left-hand side of an
> assignment, it means that the contents of the array are the target of the
> assignment rather than a reference to the array..."

The slice operator is specifically `[]` or `[1..2]`. And you can think of `[]` as being the same as `[0..$]`, where $ is the array length.

The index operator is `[1]` for example. It doesn't return a slice of the original array, it returns a single element from the array. That's indexing, not slicing.

Perhaps there should be better documentation about the difference between the two though.

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

No branches or pull requests

1 participant