dmd 2.063 beta 5

Don turnyourkidsintocash at nospam.com
Thu May 23 09:01:55 PDT 2013


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.



More information about the Digitalmars-d-announce mailing list