dmd 2.063 beta 5

Leandro Lucarella luca at llucax.com.ar
Mon May 27 10:04:59 PDT 2013


Dicebot, el 23 de May a las 16:42 me escribiste:
> 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;
> 			// ...
> 		}
> 	}
> }

You can achieve the same with:

		if (!IPv6)
			etherType = 0x0800;
		else
			...

There is no need to double-initialize a immutable value. If you want to
make it more explicit, just use something like:

enum defaultEtherType = 0x0800;

                if (!IPv6)
                        etherType = defaultEtherType;
                else
                        ...

I don't think that very rare use case, which is perfectly covered by
this "workaround" justifies the complexity added by this
double-initialization. Also code review becomes a nightmare, when I see
immutable something = 1; I can't assume something will be always 1,
I have to take a look at the whole code looking for constructors. That
SUCKS.

-- 
Leandro Lucarella (AKA luca)                     http://llucax.com.ar/
----------------------------------------------------------------------
GPG Key: 5F5A8D05 (F8CD F9A7 BF00 5431 4145  104C 949E BFB6 5F5A 8D05)
----------------------------------------------------------------------
Pa' ella cociné, pa' ella lavé, pa' ella soñe
Paella completa, $2,50
Pero, la luz mala me tira, y yo? yo soy ligero pa'l trote
La luz buena, está en el monte, allá voy, al horizonte


More information about the Digitalmars-d-announce mailing list