On Thursday, 23 May 2013 at 14:42:27 UTC, Dicebot wrote:
something I may have actually used in real code writing a low-level networking library:

struct Packet
{
        immutable etherType = 0x0800; // IPv4 by default;
        
        // ...
        
        this(bool IPv6)
        {
                if (!IPv6)
                        return; // fine with default, same as Packet.init
                else
                {
                        etherType = 0x86DD;
                        // ...
                }
        }
}

void main()
{
        auto buffer = cast(ubyte[])(Packet(true));
}

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;
  ...
}

That only leaves the case where you are bypassing the constructor.
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.

Reply via email to