digitalmars.D - Re: rtti cast
- terranium <spam here.lot> May 07 2008
- BCS <BCS pathlink.com> May 07 2008
- BCS <BCS pathlink.com> May 07 2008
- "Jarrett Billingsley" <kb3ctd2 yahoo.com> May 07 2008
- terranium <spam here.lot> May 08 2008
- BCS <ao pathlink.com> May 08 2008
Jarrett Billingsley Wrote:I perform downcasts extremely rarely.
if(auto y = cast(Y)x) ... else // not a Y, try something else
Furthermore, it's a lot easier and more efficient to have the cast return null and throw an exception _only if needed_ than to throw an exception and then have to catch it:
most programmers won't bother to thow an exception in this case, they just cast and go.For that matter, some languages (like C#) have both kinds of casts - one that throws an exception and one that doesn't. Either can really be implemented in the other, but the null-returning kind is more basic and efficient.
May 07 2008
terranium wrote:Jarrett Billingsley Wrote:I perform downcasts extremely rarely.
That's *exactly* why you need safe cast.
How does that follow? I don't see the connection.if(auto y = cast(Y)x) ... else // not a Y, try something else
yeah, try something else, hide the bug :)
That's not a bug. That is the normal idiom for checking to see if x is a Y and acting on it.Furthermore, it's a lot easier and more efficient to have the cast return null and throw an exception _only if needed_ than to throw an exception and then have to catch it:
most programmers won't bother to thow an exception in this case, they just cast and go.
And this will result in a Seg-V. The program will stop in a predictable manner. IMHO (and it seems others opinions) that is what matters.For that matter, some languages (like C#) have both kinds of casts - one that throws an exception and one that doesn't. Either can really be implemented in the other, but the null-returning kind is more basic and efficient.
...efficient bugmaker.
the same can be said going the other way. IMHO any code that take a
May 07 2008
the same can be said going the other way.
dang non-linear editing (scratch the following):IMHO any code that take a
May 07 2008
"terranium" <spam here.lot> wrote in message news:fvs67v$1gge$1 digitalmars.com...That's *exactly* why you need safe cast. ... yeah, try something else, hide the bug :) ... ...efficient bugmaker.
I _really_ don't understand where you're coming from here. I'd be interested to see some example of code where an invalid downcast were a bug and where a failed cast _should_ throw an exception. I've only ever used downcasts in D as a replacement for Java's "instanceof", where it's just a simple typecheck.
May 07 2008
Jarrett Billingsley Wrote:yeah, try something else, hide the bug :) ... ...efficient bugmaker.
I _really_ don't understand where you're coming from here. I'd be interested to see some example of code where an invalid downcast were a bug and where a failed cast _should_ throw an exception. I've only ever used downcasts in D as a replacement for Java's "instanceof", where it's just a simple typecheck.
void GetNodes() { auto obj = Parser.GetNextObject(); MyList.Add(cast(INode)obj); }
May 08 2008
Reply to terranium,Jarrett Billingsley Wrote:yeah, try something else, hide the bug :) ... ...efficient bugmaker.
interested to see some example of code where an invalid downcast were a bug and where a failed cast _should_ throw an exception. I've only ever used downcasts in D as a replacement for Java's "instanceof", where it's just a simple typecheck.
{ auto obj = Parser.GetNextObject(); MyList.Add(cast(INode)obj); }
Dang, you just reminded me that I do have some code that downcasts and expects stuff to work (it check the cast though). It's in a generated parser I'm working on.
May 08 2008