Why does SysTime have opAssign?

FeepingCreature feepingcreature at gmail.com
Wed Jun 27 12:44:09 UTC 2018


The overloaded opAssign in SysTime makes it unusable with 
Nullable in structs that have invariants that fail on T.init and 
hence @disable this().

struct S
{
   SysTime st_;

   int i;
   invariant { assert(i > 0); }
   this(int i) { this.i = i; }
   @disable this(); // S() is not a valid S
}

Nullable!S ns;
^ this fails - SysTime has opAssign and could thus observe the 
invalid .init state.

Nullable!T is constructable in one of two cases: first, if T() is 
a valid value of T, in which case the Nullable can initialize its 
member to T(),
and second, if T does not overload opAssign, in which case there 
is no way for it to notice its invalid state.
SysTime overloads opAssign, so S overloads opAssign, so S's 
opAssign call would trigger the invariant.
So S is unusable in Nullables, all because SysTime has opAssign.

Which is ...

why?



More information about the Digitalmars-d mailing list