| Thread overview | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
October 31, 2007 Minor extension to cast syntax | ||||
|---|---|---|---|---|
| ||||
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 | ||||
October 31, 2007 Re: Minor extension to cast syntax | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Mike | 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.
| |||
October 31, 2007 Re: Minor extension to cast syntax | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Mike |
"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
| |||
October 31, 2007 Re: Minor extension to cast syntax | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Mike | 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 | |||
October 31, 2007 Re: Minor extension to cast syntax | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Gregor Richards | 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
| |||
October 31, 2007 Re: Minor extension to cast syntax | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Janice Caron | 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?
| |||
October 31, 2007 Re: Minor extension to cast syntax | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Bruce Adams | 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 | |||
November 01, 2007 Re: Minor extension to cast syntax | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Janice Caron | 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.
| |||
Copyright © 1999-2021 by the D Language Foundation
Permalink
Reply