D2.0: an example of use-case for casting invariant away

Don Clugston dac at nospam.com.au
Sun Jun 24 23:27:44 PDT 2007


Jason House wrote:
> James Dennett wrote:
>> Jason House wrote:
>>> Don Clugston wrote:
>>>> int* d = cast(break const) b;
>>>>
>>>> IMHO, we want something that looks really nasty.
>>> Based on all the discussion in this thread, I like this alternative
>>> best.  I'd only insist that if someone did int *d = cast(int*) b; that
>>> the compiler would issue an error saying to use cast(break const) 
>>> instead
>>
>> Interesting: my experience suggests that it shouldn't do so.
>> Those who know enough to use cast(break const) appropriately
>> will find out how to do so.  Others would just read the error
>> message and blindly do what it said, pausing only briefly to
>> wonder why the compiler issued an error message if it knew
>> exactly how to "fix" the code.  Far too many programmers'
>> first reaction to type warnings/errors is to add casts and
>> mask the problem rather than addressing root cause.
>>
>> -- James
> 
> ... And what about those programmers who know what they're doing but 
> don't know the ins and outs of the language perfectly?  Should they 
> start scouring the documentation and news groups to find the proper fix?

In general, I agree with you, but remember that in this case, cast(break const) 
is an ugly hack which leads you into undefined behaviour. It's not like C++'s 
const_cast<> which is well-defined.

I don't think we want Microsoft's paperclip:
"You like you're trying to hang yourself, would you some help?". <g>
Hopefully breaking const is very, very rare. If a novice program actually needs 
to break const in ordinary situations, there's a big language problem.



More information about the Digitalmars-d mailing list