Bug 11984 – not using opAssign when declaration in different scope

Status
RESOLVED
Resolution
INVALID
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2014-01-24T09:04:00Z
Last change time
2014-02-19T23:35:21Z
Assigned to
nobody
Creator
luka8088

Comments

Comment #0 by luka8088 — 2014-01-24T09:04:14Z
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(); }
Comment #1 by k.hara.pg — 2014-01-25T09:34:11Z
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()]); }