www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 11984] New: not using opAssign when declaration in different scope

reply d-bugmail puremagic.com writes:
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



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: -------
Jan 24 2014
next sibling parent d-bugmail puremagic.com writes:
https://d.puremagic.com/issues/show_bug.cgi?id=11984




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: -------
Jan 25 2014
prev sibling parent d-bugmail puremagic.com writes:
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: -------
Feb 19 2014