Minor extension to cast syntax

Mike vertex at gmx.at
Wed Oct 31 10:37:29 PDT 2007


Hi!

There's some minor thing about the cast syntax that would be very nice to have. I haven't seen this mentioned here anytime, so ...

cast ( Type ) UnaryExpression

Now, the thing is that casts usually look like ...

cast(type)(some_expression);

... at least in my source code. Most casts need the second pair of parentheses. So I suggest an additional casting syntax:

cast ( Type, UnaryExpression )

like the VB.Net CStyle ... that thing would be easier to read in complex expressions (like casting an int to float for some calculation then casting it back to int, like in drawing stuff that doesn't need to be that accurate.

cast(int)(cast(float)(width - length * position) * scaling);
cast(int, cast(float, width - length * position) * scaling);

After years of writing/reading code you get a pretty good feeling for matching parentheses when you look at a line of code, but those closing parentheses in the first line always confuse the eye when you scan the line.

Personally I often forget some parentheses in casts or I find out that I need a cast after I wrote the expression. Placing the missing parentheses in the first line is just needlessly complicated. On the other hand the current cast syntax is perfect when you have a single ... uhm "piece" of expression at the right hand side of the cast operator, so having the choice between those two forms would be nice.

Hope that makes sense

-Mike



More information about the Digitalmars-d mailing list