safer casts - take II

Yigal Chripun yigal100 at gmail.com
Tue May 13 15:39:14 PDT 2008


Jason House wrote:
> Long ago in one of the const threads, a few people settled on
> cast(break const) for what I think you're calling cast!(T). I like
> that better than using template syntax to mean less safe.
> 

That's a good point I want to address:
initially this is exactly what I proposed, albeit with some minor
changes to syntax. [that was: cast(!const) where the ! was meant to be a
"not" similar to your break.

the problem is that it's not flexible enough.
how do you cast the following:
    const(invariant(C)*)*  ==> const(C*)* ?
this is a contrived example of course, but with cast!(T) you can just use:
    auto newVal = cast!(const(C*)*)oldVal;
this works since this passes the following simple test:
if you remove all invariant/const modifiers in both the source and
target types you need to get the exact same type. this is to make sure
you only change constancy and not the type. in the above case you'll
get: ((C)*)*  ==> (C*)* and those two are identical.
this only changes constancy and thus legal. had you tried to also change
type you'd get an exception.

with cast(break const) or cast(!const) this is not possible with just
one cast. that kind of shortcut may be considered as well, but since you
can always just specify the new constancy of the type this adds very
little benefit but adds another syntax rule to the language. I think
that Walter probably won't agree to add such a syntax rule and bloat the
language without proper justification. with this I 100% sure that I
prefer the minimalist approach and do not want to include this in the
proposal.

feel free to discuss this issue too and suggest use-cases where you
think this syntax is needed.



More information about the Digitalmars-d mailing list