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

Walter Bright newshound1 at digitalmars.com
Thu Jun 21 10:55:42 PDT 2007


Don Clugston wrote:
> Walter Bright wrote:
>> No, you're not missing something. It is a general problem with cast - 
>> cast is a blunt instrument which can easily hide problems.
> This means that cast() has just become even more unsafe. So for 2.0, it 
> will be even more important to provide ways to avoid usage of cast().
> The .ptr, .re, and .im properties were a huge help; maybe the idea can 
> be extended to other cases where a cast is perfectly safe.

I think you're right.

> Usage of invariants inside unions is suspect too.
> At least where some members are invariant and others are not const, it's 
> asking for trouble -- cast() by stealth.
> 
> union U {
>  invariant C *c;
>  C *d;
> }
> Though arguably unions are always a blunt, dangerous instrument as well.

This is why some languages disallow unions completely. But I'm not 
concerned about this case, as it's a deliberate attempt to circumvent 
the typing rules. If someone is doing it deliberately, D gives them the 
benefit of the doubt and presumes they know what they're doing. I'm only 
concerned about the former inadvertent case.



More information about the Digitalmars-d mailing list