D - typesafe cast
- Pavel Minayev (21/21) Feb 17 2002 Walter, pleeeease make some sort of cast that'd throw an exception
- Russell Borogove (9/21) Feb 17 2002 I was going to suggest that assert (or something very
- Pavel Minayev (10/17) Feb 17 2002 No, it doesn't (remember the discussion of whether expressions
- Russell Borogove (5/20) Feb 17 2002 It doesn't state so clearly in the "Contracts" section of
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 doesn't state so clearly in the "Contracts" section of the current spec, but I'll take your word for it.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).Agreed. -RB
Feb 17 2002