Is this really intended??

claptrap clap at trap.com
Sun Oct 11 00:16:53 UTC 2020


struct Foo
{
     int i;
     void opAssign(int rhs) { this.i = rhs; }
     void reset() { i = 0; }
}

class Bar
{
     Foo foo;

     this()
     {
         foo.i = 0;    // *1
         foo.reset();  // *2
         foo = 42;
     }
}

With *1 & *2 commented out...

onlineapp.d(19): Error: cannot implicitly convert expression x of 
type int to Foo
onlineapp.d(19):        this.foo = x is the first assignment of 
this.foo therefore it represents its initialization
onlineapp.d(19):        opAssign methods are not used for 
initialization, but for subsequent assignments

With *2 commented out it compiles with no errors

With *1 commented out...

onlineapp.d(19): Error: cannot implicitly convert expression x of 
type int to Foo
onlineapp.d(19):        this.foo = x is the first assignment of 
this.foo therefore it represents its initialization
onlineapp.d(19):        opAssign methods are not used for 
initialization, but for subsequent assignments

Arn't structs supposed to be default initialised?

But in a constructor a struct member is not? If you write to a 
field in a struct member suddenly it is considered initialised?

And yet it's OK to call a method on the struct that is not 
initialised, but if you do it's still uninitialised?

Why should

foo.i = 0;
foo.reset();

Result in the compiler changing whether it's OK to call opAssign?

That's some weird ****.







More information about the Digitalmars-d mailing list