Safer casts

Robert Fraser fraserofthenight at gmail.com
Fri May 9 11:23:41 PDT 2008


Robert Fraser wrote:
>  [...]

Also, I find the cast-returns-null-on-failure behavior quite useful. In 
your system, this piece of code:

Shape s = getShape();
if(auto r = cast(Rectangle) s) {
     return r.length * r.width;
}
else if(auto c = cast(Circle) s) {
     return PI * c.radius * c.radius;
}

... would become ...

Shape s = getShape();
if(is!(Rectangle)(s)) {
     auto r = class!(Rectangle) s;
     return r.length * r.width;
}
else if(is!(Rectangle)(s)) {
     auto c = class!(Circle) s;
     return PI * c.radius * c.radius;
}

Which is more typing and uglier IMO. Since if you want this behavior, 
you can already make templates to do it, why force it down people's throats?



More information about the Digitalmars-d mailing list