What is the case against a struct post-blit default constructor?

Era Scarecrow rtcvb32 at yahoo.com
Fri Oct 12 13:33:09 PDT 2012


On Friday, 12 October 2012 at 08:20:42 UTC, Jonathan M Davis 
wrote:

> Really, I think that it's a bad design decision to require that 
> the invariant be called before opAssign. It does _not_ play 
> nice with some of D's other features, and the result is likely 
> to be that invariants get used less, meaning that code is more 
> likely to be buggy.

  You make a good argument, but you can also override opAssign for 
things that are not it's type exact type. So...

  struct S {
    float x;

    ref S opAssign(int y) {
      x = y;
      return this;
    }
  }

  S s;
  int i;
  s = i; //opAssign, correct?


  In cases like this opAssign would need an invariant before and 
after the call. But if you were just replacing the whole object 
you wouldn't.

  I'll say a @novariant is the better answer, and automatically 
used on the default copy/opAssign/postblitz (before the call, but 
still needed after).


More information about the Digitalmars-d mailing list