Article about problems & suggestions for D 2.0

Walter Bright newshound2 at digitalmars.com
Sun Aug 28 13:17:11 PDT 2011


On 8/28/2011 12:44 PM, David Nadlinger wrote:
> It is already in D, and in an, in my personal opinion, much worse way: You can
> just cast away const/immutable with cast().

True, but such is explicitly undefined behavior, and is not allowed in safe mode.


> What do you propose for the situations where you need to cast away const? (Yes
> you'll find yourself in one of them from time to time in the real world, e.g.
> when dealing with legacy/C code…)

That's why D does allow this, in a user visible manner, rather than forcing one 
to do it in a backdoor manner (like using inline asm).


> I know that this is not the general consensus, but I very much like the C++
> casting operators, because you can quickly get a rough idea what is going on
> when you see a cast in the code, whereas for D, cast() be anything between a
> perfectly harmless downcast (if checking for null, obviously), changing the
> storage class (const/immutable/shared), or causing the bytes stored to be
> interpreted in a completely different way!

To cast away const, use:

    cast()expr

i.e. there is a special syntax for it.


More information about the Digitalmars-d mailing list