Thread overview
[Issue 11984] New: not using opAssign when declaration in different scope
Jan 24, 2014
luka8088
Jan 25, 2014
Kenji Hara
Feb 20, 2014
Kenji Hara
January 24, 2014
https://d.puremagic.com/issues/show_bug.cgi?id=11984

           Summary: not using opAssign when declaration in different scope
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: nobody@puremagic.com
        ReportedBy: luka8088@owave.net


--- Comment #0 from luka8088 <luka8088@owave.net> 2014-01-24 09:04:14 PST ---
I am sorry for the (probably) misleading summary but I am myself unsure how to call it. If S1!(C[]) s1c; declaration is in the constructor then it works, if it is a class member then the following error pops up:

Error: cannot implicitly convert expression ([new C]) of type C[] to S1!(C[])

-------------------------------

import std.stdio;

struct S1 (T) {

  T v1;

  void opAssign (T v2) {
    v1 = v2;
  }

}

class C {
}

class Broken {

  S1!(C[]) s1c;

  this () {

    static if (__traits(compiles, s1c = [new C()]))
      s1c = [new C()];
    writeln(s1c.v1.length);

  }

}

class Works {

  this () {

    S1!(C[]) s1c;
    s1c = [new C()];
    writeln(s1c.v1.length);

  }

}

void main () {
  new Broken();
  new Works();
}

-- 
Configure issuemail: https://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
January 25, 2014
https://d.puremagic.com/issues/show_bug.cgi?id=11984



--- Comment #1 from Kenji Hara <k.hara.pg@gmail.com> 2014-01-25 09:34:11 PST ---
This is an intended behavior. In short, field assignment expression inside constructor is specially translated to the field initialization.

https://d.puremagic.com/issues/show_bug.cgi?id=9665 http://dlang.org/class#field-init

In your code, S1!C is not directly initializable by C[] value, so

> class Broken {
> 
>   S1!(C[]) s1c;
> 
>   this () {
>       s1c = [new C()];
>   }
> }

Should be rewritten to:

    this () {
        s1c = S1!C([new C()]);
    }

-- 
Configure issuemail: https://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
February 20, 2014
https://d.puremagic.com/issues/show_bug.cgi?id=11984


Kenji Hara <k.hara.pg@gmail.com> changed:

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


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