Thread overview
[Issue 2730] New: Restriction on op= can be lifted
Mar 12, 2009
d-bugmail
Mar 21, 2009
d-bugmail
Apr 01, 2009
d-bugmail
March 12, 2009
http://d.puremagic.com/issues/show_bug.cgi?id=2730

           Summary: Restriction on op= can be lifted
           Product: D
           Version: 2.025
          Platform: PC
               URL: http://www.digitalmars.com/d/2.0/operatoroverloading.htm
                    l#Array
        OS/Version: Linux
            Status: NEW
          Severity: normal
          Priority: P2
         Component: www.digitalmars.com
        AssignedTo: bugzilla@digitalmars.com
        ReportedBy: jlquinn@optonline.net


The section on opIndex and opIndexAssign states that op=, ++, and -- operators can't be supported.

However, if we have:

struct S {

 ref <type> opIndex(size_t idx) {...}

}

then, the following code will do what we expect

S s;
s[i] += 4;

as the += ends up calling opIndex.  At least, this is what dmd 2.026 appears to do.  If the docs are adjusted accordingly, then that wart is removed.

As a side note, opIndexAssign doesn't appear necessary at all if we do things this way.  As long as opIndex is declared with ref, it can be used as lvalue and rvalue.


-- 

March 21, 2009
http://d.puremagic.com/issues/show_bug.cgi?id=2730


smjg@iname.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |smjg@iname.com




------- Comment #1 from smjg@iname.com  2009-03-20 20:10 -------
opIndexAssign will always be needed for something.  Library implementations of AAs and bit arrays are an example.

There should be probably be a choice of defining opIndexAssign or defining opIndex with a ref return.  opIndexAssign could even work for the example code, if s[i] += 4 were rewritten as s[i] = s[i] + 4 (with i being evaluated only once) and then expanded into op functions.

The tricky bit is deciding on the logic to use if both are defined....


-- 

April 01, 2009
http://d.puremagic.com/issues/show_bug.cgi?id=2730


bugzilla@digitalmars.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|                            |FIXED




------- Comment #2 from bugzilla@digitalmars.com  2009-04-01 13:56 -------
Fixed DMD 2.027


--