www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Minor extension to cast syntax

reply Mike <vertex gmx.at> writes:
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
Oct 31 2007
next sibling parent Robert Fraser <fraserofthenight gmail.com> writes:
Mike Wrote:

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

First one looks cleaner to me, but maybe that's just because I'm used to it. I'm not against adding the syntax, but I'd never use it.
Oct 31 2007
prev sibling next sibling parent "Steven Schveighoffer" <schveiguy yahoo.com> writes:
"Mike" wrote
 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 )

Mike, This sounds like a matter of personal preference. Since you can already achieve the same thing via the current syntax (with very little change), it would seem that the cost/reward ratio is awfully high. -Steve
Oct 31 2007
prev sibling parent reply Gregor Richards <Richards codu.org> writes:
Blech. Makes it look like you're calling a function with arguments being 
a type and then some expression. The current syntax is more similar to 
calling a templated cast function:

cast(int)(foo)
cast!(int)(foo)

That is, cast is a function with a template parameter of the type you're 
casting it to. This isn't how it's implemented at all of course, but the 
syntax is reminiscent of that, and I think that's the better analogy.

  - Gregor Richards
Oct 31 2007
next sibling parent reply "Janice Caron" <caron800 googlemail.com> writes:
On 10/31/07, Gregor Richards <Richards codu.org> wrote:
 The current syntax is more similar to
 calling a templated cast function:

 cast(int)(foo)
 cast!(int)(foo)

 That is, cast is a function with a template parameter of the type you're
 casting it to. This isn't how it's implemented at all of course, but the
 syntax is reminiscent of that, and I think that's the better analogy.

Indeed. And in D2, we've now also got to!(int)(foo) which follows the same pattern
Oct 31 2007
parent reply Bruce Adams <tortoise_74 yeah.who.co.uk> writes:
Janice Caron Wrote:

 On 10/31/07, Gregor Richards <Richards codu.org> wrote:
 The current syntax is more similar to
 calling a templated cast function:

 cast(int)(foo)
 cast!(int)(foo)

 That is, cast is a function with a template parameter of the type you're
 casting it to. This isn't how it's implemented at all of course, but the
 syntax is reminiscent of that, and I think that's the better analogy.

Indeed. And in D2, we've now also got to!(int)(foo) which follows the same pattern

to is a difficult word to google. The shriek doesn't help. Would someone care to enlighten me as to precisely what to does and how a "to" conversion differs from a cast?
Oct 31 2007
parent Bruce Adams <tortoise_74 yeah.who.co.uk> writes:
Janice Caron Wrote:

 On 10/31/07, Bruce Adams <tortoise_74 yeah.who.co.uk> wrote:
 Indeed. And in D2, we've now also got

 to!(int)(foo)

 which follows the same pattern

to is a difficult word to google. The shriek doesn't help. Would someone care to enlighten me as to precisely what to does and how a "to" conversion differs from a cast?

See http://digitalmars.com/d/phobos/std_conv.html

Thanks.
Oct 31 2007
prev sibling parent "Janice Caron" <caron800 googlemail.com> writes:
On 10/31/07, Bruce Adams <tortoise_74 yeah.who.co.uk> wrote:
 Indeed. And in D2, we've now also got

 to!(int)(foo)

 which follows the same pattern

to is a difficult word to google. The shriek doesn't help. Would someone care to enlighten me as to precisely what to does and how a "to" conversion differs from a cast?

See http://digitalmars.com/d/phobos/std_conv.html
Oct 31 2007