D - typesafe cast
- "Pavel Minayev" <evilone omen.ru> Feb 17 2002
- Russell Borogove <kaleja estarcion.com> Feb 17 2002
- "Pavel Minayev" <evilone omen.ru> Feb 17 2002
- Russell Borogove <kaleja estarcion.com> Feb 17 2002
Walter, pleeeease make some sort of cast that'd throw an exception
(preferrabely with file/line info =)) when actual object type doesn't
match the desired. In other words, a usual typecast with implicit
assert. I asked this before, mostly for "theoretical" reasons, but
now it turned out to be quite a practical feature. WinD is just 2500
lines long, and it already has about 15 pieces of code, like this:
...
Control ctl = cast(Control) parent();
assert(ctl);
ScreenToClient(ctl.handle(), &pt);
...
Variable ctl is used only once. Thus, in such cases, it just clutters
code. It'd look much better if there was a cast throwing exception
on error, for example:
...
ScreenToClient((parent() as Control).handle(), &pt);
...
As you can see, my proposal is "<expr> as <type>", but it could be
pretty much anything... another idea:
cast[type] expr
What do you think of all this?
Feb 17 2002
Pavel Minayev wrote:Walter, pleeeease make some sort of cast that'd throw an exception (preferrabely with file/line info =)) when actual object type doesn't match the desired. In other words, a usual typecast with implicit assert. I asked this before, mostly for "theoretical" reasons, but now it turned out to be quite a practical feature. WinD is just 2500 lines long, and it already has about 15 pieces of code, like this: ... Control ctl = cast(Control) parent(); assert(ctl); ScreenToClient(ctl.handle(), &pt); ...
I was going to suggest that assert (or something very like it) evaluate and _return_ its argument/operand in all build versions, so you could write: ScreenToClient( assert( cast( Control ) parent() ), &pt ); It's a little verbose, but not a three-liner this way. (Does D's assert() test in non-debug versions? The documentation isn't explicit about it.) -RB
Feb 17 2002
"Russell Borogove" <kaleja estarcion.com> wrote in message news:3C6FFA4F.9070702 estarcion.com...I was going to suggest that assert (or something very like it) evaluate and _return_ its argument/operand in all build versions, so you could write: ScreenToClient( assert( cast( Control ) parent() ), &pt ); It's a little verbose, but not a three-liner this way. (Does D's assert() test in non-debug versions? The documentation isn't explicit about it.)
No, it doesn't (remember the discussion of whether expressions with side-effects should be forbidden from assert?). It is supposed to help you debug the program, just like in/out blocks, invariants, unittests etc... in release they're all stripped (and I believe the documentation states this quite clearly =)). Also, this syntax is sooo lengthy... something better to avoid for a typical operation (and my experience shows that typesafe casting is frequently used in strongly-typed OOP environment).
Feb 17 2002
Pavel Minayev wrote:"Russell Borogove" <kaleja estarcion.com> wrote in message news:3C6FFA4F.9070702 estarcion.com...It's a little verbose, but not a three-liner this way. (Does D's assert() test in non-debug versions? The documentation isn't explicit about it.)
No, it doesn't (remember the discussion of whether expressions with side-effects should be forbidden from assert?). It is supposed to help you debug the program, just like in/out blocks, invariants, unittests etc... in release they're all stripped (and I believe the documentation states this quite clearly =)).
It doesn't state so clearly in the "Contracts" section of the current spec, but I'll take your word for it.Also, this syntax is sooo lengthy... something better to avoid for a typical operation (and my experience shows that typesafe casting is frequently used in strongly-typed OOP environment).
Agreed. -RB
Feb 17 2002








Russell Borogove <kaleja estarcion.com>