Article: Why Const Sucks

joe joe at example.com
Thu Mar 8 14:58:21 UTC 2018


On Monday, 5 March 2018 at 10:57:35 UTC, Jonathan M Davis wrote:
> Here's something I wrote up on const:
> /snip

May be not entirely related, but a little gotcha.

given:

interface XY {}

class Foo: XY {}
class Bar: XY {}

void doSomething(in XY sth)
{
   auto foo = cast(Foo)sth; // error in @safe code
}

But the compiler doesn't emit a warning that const got cast away 
in @system code.
Since @system is the default setting and casting const away is by 
definition undefined behavior, there should be some feedback.

If you cast away const in C++ you need to be explicit about it by 
using const_cast, documents the intention rather than D's 
swiss-army-cast.


More information about the Digitalmars-d-announce mailing list