Safer casts

Yigal Chripun yigal100 at gmail.com
Sun May 11 07:43:36 PDT 2008


Janice Caron Wrote:

> On 11/05/2008, Yigal Chripun <yigal100 at gmail.com> wrote:
> > another option would be to allow:
> >  cast(const(T))x;
> >  where you redefine the constancy of x and this would be subject to a
> >  test that T is not a different type (except for constancy, of course).
> 
> Oh, I see. So, cast(T) would double up for /either/ downcasting, /or/
> removing constancy, but never both at the same time. (Both at the same
> time would require two casts).
> 
> This is probably the best suggestion of all. I like it.

Actually, we got it slightly wrong: 
since D defines removal of invariance as undefined behavior and since casting with constancy does not convert the value but rather changes the type tag of the value, it would be more appropriate to use the "cast!" form, IMO.
it feels more appropriate to me cause it warns the user that it's his responsibility if something breaks. That's the entire reason for the const! form, isn't it?

--Yigal



More information about the Digitalmars-d mailing list