Bug 14935 – [Operator Overloading] Wrong description on overloading a[]

Status
RESOLVED
Resolution
INVALID
Severity
enhancement
Priority
P3
Component
dlang.org
Product
D
Version
D2
Platform
All
OS
All
Creation time
2015-08-19T12:41:00Z
Last change time
2015-08-20T15:58:32Z
Assigned to
nobody
Creator
r.97all

Comments

Comment #0 by r.97all — 2015-08-19T12:41:43Z
The code in http://dlang.org/operatoroverloading.html#slice : struct S { int[] impl; int[] opIndex() { return impl[]; } } void test() { auto s = S([1,2,3]); auto t = s[]; // calls s.opIndex() assert(t == [1,2,3]); } doesn't work. When I changed opIndex to opSlice it worked. I suspect that 'opIndex' reads 'opSlice' in the description "To overload a[], simply define opIndex with no parameters:".
Comment #1 by ag0aep6g — 2015-08-19T12:47:56Z
Works for me with 2.068.0. What version of the compiler are you using? I think things changed from opSlice to opIndex somewhat recently.
Comment #2 by r.97all — 2015-08-19T13:05:23Z
The version I used is 2.067. If this is a change it must be listed on ChangeLog anyway, but I couldn't find it. The document goes 'opIndex' for about an year, since this commit: https://github.com/D-Programming-Language/dlang.org/commit/4af3111a5d1efb9dc79638d69bf87d9757a6a37b#diff-87ae512b433ac9f86b715f03fa17cb0eR691
Comment #3 by ag0aep6g — 2015-08-19T13:16:14Z
(In reply to Ryuichi OHORI from comment #2) > The version I used is 2.067. Works for me with 2.067, too. What error are you getting?
Comment #4 by r.97all — 2015-08-19T13:31:43Z
Ah, it seems I mis-minimized my code using older ones in http://dpaste.dzfl.pl/ Here is an example 2.065 doesn't compile: http://dpaste.dzfl.pl/fork/d52a0e0a2163 I'm not sure but I was getting the same error....
Comment #5 by ag0aep6g — 2015-08-19T13:57:38Z
(In reply to Ryuichi OHORI from comment #4) > Ah, it seems I mis-minimized my code using older ones in > http://dpaste.dzfl.pl/ > Here is an example 2.065 doesn't compile: > http://dpaste.dzfl.pl/fork/d52a0e0a2163 > I'm not sure but I was getting the same error.... So the code works for you with a recent compiler (2.067, 2.068)? I think code in the online docs doesn't need to be compatible with 2.065. So I'm closing this as invalid. Please reopen if the code doesn't work for you with 2.068, or generally if you disagree with me closing. If you're getting that "cannot be sliced with []" error with different, valid code, then please file a new bug.
Comment #6 by r.97all — 2015-08-20T12:57:54Z
I agree with you on closing this as invalid. The main problem was that I missed opIndexOpAssign and tried to use opIndex/opSlice (of a struct S) + opOpAssign (of the returning Voldemort type of S.opIndex). While I went wrong, the error message "cannot be sliced with []" is not so helpful as to help me figuring out the problem. One of the reasons I missed opIndexOpAssign was that it is categorized under Op Assignment Operator Overloading. I'd like to place it under Array Indexing and Slicing Operators Overloading.
Comment #7 by ag0aep6g — 2015-08-20T15:58:32Z
(In reply to Ryuichi OHORI from comment #6) > I agree with you on closing this as invalid. The main problem was that I > missed opIndexOpAssign and tried to use opIndex/opSlice (of a struct S) + > opOpAssign (of the returning Voldemort type of S.opIndex). > > While I went wrong, the error message "cannot be sliced with []" is not so > helpful as to help me figuring out the problem. If I understand correctly, you're describing something like this: ---- struct V { void opOpAssign(string op)(int x) {} } struct S { V opIndex() {return V();} } void main() { S s; s[] += 2; /* Error: S cannot be sliced with [] */ } ---- That error message really doesn't help. And it actually works when you use a temporary variable to hold the result of s[]: ---- S s; auto v = s[]; v += 2; /* no error */ ---- So I'd say that's a rejects-valid bug. I filed issue 14941 for that. > One of the reasons I missed opIndexOpAssign was that it is categorized under > Op Assignment Operator Overloading. I'd like to place it under Array > Indexing and Slicing Operators Overloading. I don't know where it fits best. Maybe just make a pull request and see how it goes.