Safer casts

terranium spam at here.lot
Sat May 10 03:01:17 PDT 2008


Robert Fraser Wrote:

> 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;
> }
> 

... should become ... :))))

Shape s = getShape();
return s.square();



More information about the Digitalmars-d mailing list