Casting away const and invariant

Janice Caron caron800 at googlemail.com
Sun Sep 9 06:56:51 PDT 2007


You have misunderstood my suggestion. Please stop implying that I have
not read Walter's documentation. It is a little insulting.

Suppose there exists a function with a prototype something like

void makeUppercase(ubyte[] s);

It's declared as ubyte[], not char[], correctly, because it's intended
for some encoding other than UTF-8. It does its conversion in place.

Now suppose that a user of that library at first writes this:

string s;
makeUppercase(s);

The compiler will correctly complain that you can't cast from string
to ubyte[]. So now, to fix that, suppose that the writer of that code
changes it to:

string s;
makeUppercase(cast(ubyte[])s);

Currently, that will compile.

What *I am suggesting* is to make it /not/ compile. This makes things
/safer/, not less safe. Do you see now?

By insisting that the /only/ way to remove const be with an explicit
cast(!const), the world becomes a safer place. (Well - maybe that last
claim was too bold! :-) )



More information about the Digitalmars-d mailing list