Accidentally killing immutable is too easy in D (Was: cast()x - a

bearophile bearophileHUGS at lycos.com
Wed Jun 1 15:13:06 PDT 2011


Jonathan M Davis:

> Personally, I find 
> how C++ created multiple types of cast (include const_cast) _highly_ annoying 
> and generally useless, and I'm _very_ glad that D didn't do anything of the 
> sort.

Conflating all casts in a single syntax makes the D language look a bit simpler, but it also reduces the amount of expressivity and I suspect it may lead to some bugs (here Unqual helps). Changing the const state of a type is often orthogonal to changing its data type.

Thankfully D templates make it easy enough to build new types of cast. Time ago I have proposed a staticObjCast! for object references that performs some static sanity tests too. Unqual!() is another kind of cast, but it's a bit blunt. std.conv.to! is another kind of cast, that performs more complex conversions too. I am proposing to use to! for safe enum conversions too. A staticCast! that uses a locally defined enum sounds useful and safe.

Bye,
bearophile


More information about the Digitalmars-d mailing list