www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Proporsal for cast syntax change

reply Juanjo =?ISO-8859-15?Q?=C1lvarez?= <juanjuxNO SPAMyahoo.es> writes:
Maybe it's too late for this, but I think:

cast( int, 34.5 );

looks a lot nicer than:

cast( int) 34.5;

less parenthesis; function call-like syntax... don't know, what do you
people think?
Aug 02 2004
next sibling parent reply Andy Friesen <andy ikagames.com> writes:
Juanjo Álvarez wrote:
 Maybe it's too late for this, but I think:
 
 cast( int, 34.5 );
 
 looks a lot nicer than:
 
 cast( int) 34.5;
 
 less parenthesis; function call-like syntax... don't know, what do you
 people think?

Suffice to say that this exact syntax has been proposed by unrelated individuals at least three times over the past year or so. :) -- andy
Aug 02 2004
parent reply Juanjo =?ISO-8859-15?Q?=C1lvarez?= <juanjuxNO SPAMyahoo.es> writes:
Andy Friesen wrote:

 Juanjo Álvarez wrote:
 Maybe it's too late for this, but I think:
 
 cast( int, 34.5 );
 
 looks a lot nicer than:
 
 cast( int) 34.5;
 
 less parenthesis; function call-like syntax... don't know, what do you
 people think?

Suffice to say that this exact syntax has been proposed by unrelated individuals at least three times over the past year or so. :)

Another reason, we've Figure Rectangle Square Now compare: cast( Figure, cast( Rectangle, Square) ); with cast(Figure) ( cast(Rectangle) Square ); Which is more readable?
Aug 03 2004
next sibling parent Andy Friesen <andy ikagames.com> writes:
Juanjo Álvarez wrote:
 Andy Friesen wrote:
 
 
Juanjo Álvarez wrote:

Maybe it's too late for this, but I think:

cast( int, 34.5 );

looks a lot nicer than:

cast( int) 34.5;

less parenthesis; function call-like syntax... don't know, what do you
people think?

Suffice to say that this exact syntax has been proposed by unrelated individuals at least three times over the past year or so. :)

Another reason, we've Figure Rectangle Square Now compare: cast( Figure, cast( Rectangle, Square) ); with cast(Figure) ( cast(Rectangle) Square ); Which is more readable?

I would argue that it doesn't matter: if you're casting a lot, you may have other problems. Bear in mind, though, that I rather like the C++ cast syntax because it's so ugly. It is, after all, an ugly concept. :) At any rate, double-casting is only ever needed when you are doing things that you absolutely should not ever do: casting object references to and from non-pointer types. (confusing the GC is a Bad Idea) -- andy
Aug 03 2004
prev sibling next sibling parent Arcane Jill <Arcane_member pathlink.com> writes:
In article <cenej0$sae$1 digitaldaemon.com>, Juanjo =?ISO-8859-15?Q?=C1lvarez?=
says...
Now compare:

cast( Figure, cast( Rectangle, Square) );

with

cast(Figure) ( cast(Rectangle) Square );

Which is more readable?

cast(Figure) Square; // Double cast achieves nothing. <sarcasm>Hey, let's copy C++: static_cast!(int)(x) dynamic_cast!(int)(x) reinterpret_cast!(int)(x) const_cast!(int)(x) </sarcasm> Arcane Jill
Aug 03 2004
prev sibling parent "Matthew" <admin stlsoft.dot.dot.dot.dot.org> writes:
"Juanjo Álvarez" <juanjuxNO SPAMyahoo.es> wrote in message
news:cenej0$sae$1 digitaldaemon.com...
 Andy Friesen wrote:

 Juanjo Álvarez wrote:
 Maybe it's too late for this, but I think:

 cast( int, 34.5 );

 looks a lot nicer than:

 cast( int) 34.5;

 less parenthesis; function call-like syntax... don't know, what do you
 people think?

Suffice to say that this exact syntax has been proposed by unrelated individuals at least three times over the past year or so. :)

Another reason, we've Figure Rectangle Square Now compare: cast( Figure, cast( Rectangle, Square) ); with cast(Figure) ( cast(Rectangle) Square );

Surprisingly, the second one, i.e. the current syntax
Aug 03 2004
prev sibling parent reply J C Calvarese <jcc7 cox.net> writes:
Juanjo Álvarez wrote:
 Maybe it's too late for this, but I think:
 
 cast( int, 34.5 );
 
 looks a lot nicer than:
 
 cast( int) 34.5;
 
 less parenthesis; function call-like syntax... don't know, what do you
 people think?

By my count, it's the same number of parentheses (and it adds a comma). Explain to me how that's a benefit. ;) Anyways, I don't see what the problem is with the current much-maligned cast syntax. It's only slightly different than the C-style, so it shouldn't be hard for C/C++ pros to learn. It doesn't require a tremendous amount of typing, and it's readable. And, as others have written, if a person is doing a lot of casting it could be the sign of a poor design. -- Justin (a/k/a jcc7) http://jcc_7.tripod.com/d/
Aug 03 2004
parent "Matthew" <admin stlsoft.dot.dot.dot.dot.org> writes:
"J C Calvarese" <jcc7 cox.net> wrote in message
news:ceptnf$1vjb$1 digitaldaemon.com...
 Juanjo Álvarez wrote:
 Maybe it's too late for this, but I think:

 cast( int, 34.5 );

 looks a lot nicer than:

 cast( int) 34.5;

 less parenthesis; function call-like syntax... don't know, what do you
 people think?

By my count, it's the same number of parentheses (and it adds a comma). Explain to me how that's a benefit. ;) Anyways, I don't see what the problem is with the current much-maligned cast syntax. It's only slightly different than the C-style, so it shouldn't be hard for C/C++ pros to learn. It doesn't require a tremendous amount of typing, and it's readable. And, as others have written, if a person is doing a lot of casting it could be the sign of a poor design.

There's nothing wrong with it, since it's uniquely greppable (and therefore supportive of automation-assisted code review). That was the major flaw with C-style casts. In terms of what it does, it can be argued that one cast to cast them all is a little careless, wrt C++'s delineation of casting responsibilities, but I can live with it because it's greppable. And your last line has it correct. I try to keep the casting abstracted into worker functions, and then use the D types in the main functional code. That works pretty well.
Aug 03 2004