safer casts - take II

Bill Baxter dnewsgroup at billbaxter.com
Tue May 13 13:33:30 PDT 2008


terranium wrote:
> BCS Wrote:
> 
>> you will need to demonstrate a better way. So far no one has.
> 
> Your argument is big need for if(auto y=cast(Y)x) ?
> http://www.digitalmars.com/webnews/newsgroups.php?art_group=digitalmars.D&article_id=71729

I agree with BCS.  There's nothing wrong with the null-returning dynamic 
cast.  It's exactly what dynamic_cast does in C++.

Dynamic casts are particularly useful when working with interfaces in D, 
I think.  You've got a big pool of objects and you get one and want to 
ask "Hey, object, do you implement the IHasCheezburger interface?" so 
you just cast to IHasCheezburger and do something if it does, something 
else if it doesn't.  There is nothing "exceptional" happening there, so 
it doesn't make sense to throw an exception.

I don't really understand what you were getting at with your hashtable 
example in your other post.  But it sounds like you're saying we should 
re-implement a subset of the down-casting functionality that is already 
present in D objects instead of using the built-in dynamic cast 
capability.  Why should we do that if D already manages it for us?

--bb



More information about the Digitalmars-d mailing list