www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 11290] New: Usage of alias in opBinary on object that is passed in leads to unexpected behaviour.

http://d.puremagic.com/issues/show_bug.cgi?id=11290

           Summary: Usage of alias in opBinary on object that is passed in
                    leads to unexpected behaviour.
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: nobody puremagic.com
        ReportedBy: ajidala gmail.com



Using alias in opBinary onto the object that is passed in incorrectly aliases
to "this" instead of the object passed in. It's best illustrated with some
code:

import std.stdio;

class Foo {
    int kek;

    this(int val) {
        this.kek = val;
    }
    Foo opBinary(string op)(Foo bar) if (op == "*") {
        alias k1 = this.kek;
        alias k2 = bar.kek;
        writefln("k1 = %s, this.kek = %s", k1, this.kek);
        writefln("k2 = %s, bar.kek = %s", k2, bar.kek);

        Foo f = new Foo(k1 * k2);
        return f;
    }
}

void main() {
    Foo f1 = new Foo(2);
    Foo f2 = new Foo(5);
    Foo f3 = f1 * f2;
}

The output is:

k1 = 2, this.kek = 2
k2 = 2, bar.kek = 5

The expected output would be:

k1 = 2, this.kek = 2
k2 = 5, bar.kek = 5

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Oct 17 2013