rtti cast

Robert Fraser fraserofthenight at gmail.com
Fri May 2 16:41:21 PDT 2008


terranium wrote:
> Pragma Wrote:
> 
>> If you absolutely need an exception
> 
> And who don't? Yet another D misdesign to be worked around? And where will be more legacy design typos in the end? In C++ or in D?

In Java, I often find myself writing code like:

void process(Object o)
{
     if(o instanceof String)
     {
         String s = (String) o;
         // ...
     }
     else if(o instanceof List)
     {
         List<String> l = (List<String>) o;
         // ...
     }
}

It's much easier (and more efficient) to use the D way:

void process(Object o)
{
     if(auto s = cast(string) o)
     {
         //...
     }
     else if(auto l = cast(Seq!(string) o)
     {
         // ...
     }
}



More information about the Digitalmars-d mailing list