| Thread overview |
|---|
May 07, 2008 Re: rtti cast | ||||
|---|---|---|---|---|
| ||||
Jarrett Billingsley Wrote: > I perform downcasts extremely rarely. That's *exactly* why you need safe cast. > > if(auto y = cast(Y)x) > ... > else > // not a Y, try something else yeah, try something else, hide the bug :) > 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. ...efficient bugmaker. | ||||
May 07, 2008 Re: rtti cast | ||||
|---|---|---|---|---|
| ||||
Posted in reply to terranium | 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 Re: rtti cast | ||||
|---|---|---|---|---|
| ||||
Posted in reply to BCS | > 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 Re: rtti cast | ||||
|---|---|---|---|---|
| ||||
Posted in reply to terranium | "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 08, 2008 Re: rtti cast | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Jarrett Billingsley | 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 Re: rtti cast | ||||
|---|---|---|---|---|
| ||||
Posted in reply to terranium | Reply to terranium,
> 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);
> }
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.
| |||
Copyright © 1999-2021 by the D Language Foundation
Permalink
Reply