the behavior of opAssign

Jonathan M Davis newsgroup.d at jmdavisprog.com
Mon Jan 29 10:04:37 UTC 2018


On Monday, January 29, 2018 09:23:55 Sobaya via Digitalmars-d-learn wrote:
> I found a strange behavior.
>
> class A {
>      void opAssign(int v) {}
> }
>
> class Test {
>      A a;
>      this() {
>          a = new A(); // removing this causes compile error.
>          a = 3; // cannot implicitly convert expression `3` of
> `int` to `A`
>      }
> }
>
> void main() {
>      // this is allowed.
>      A a;
>      a = 3;
> }
>
> Is it a compiiler's bug?

I don't think so. The first "assignment" is actually initialization, not
assignment. opAssign is for assigning to a value that was already
initialized.

- Jonathan M Davis



More information about the Digitalmars-d-learn mailing list