On Thursday, 23 May 2013 at 16:01:56 UTC, Don wrote:
That's better, but it's still not a convincing example.

I don't see why you cannot remove the intializer, and write:

this(bool IPv6)
{
    if (!IPv6)
      etherType = 0x0800;
    else
      etherType = 0x86DD;
  ...
}

Because then you will have an invalid default-initialized state. For example, in my case static array of Packets can be declared and I am guaranteed to have a properly initialized packet templates there (constructor can't be used obviously). Now after some thinking I favor template-based approach specifically for this task but general principle should be clear.

That only leaves the case where you are bypassing the constructor.

Not "by-passing", more like "not getting to". That it is one of subtle and incredibly awesome D features - T.init

If you have a constructor, but have just used Packet.init, the object is not constructed properly. I cannot see the value in having etherType initialized and everything else not.

I have not mentioned other fields but they are expected to be initialized too, thanks to CTFE (don't know how it inter-operates with unions in current DMD though). So Packet.init here becomes _completely constructed_ Packet with defaults computed at compile-time.

Reply via email to