www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Proposal Cast Syntax

reply Stephan Wienczny <Stephan Wienczny.de> writes:
Hallo,

I like the new way of casting except on thing: It could be a little bit 
more precise. Could we have something like this:

cast(BasicType, valuetocast)

A cast is IMHO nothing else than a special function, why shouldn't we 
handle it as such? The advantage of this style can be seen if you cast 
classes with members:

cast(BasicType,valuetocast).foo instead of
(cast(BasicType)valuetocast).foo

You don't need the extra parenthesis arround the cast anymore.

Stephan
Jul 02 2004
next sibling parent reply Trejkaz Xaoza <trejkaz xaoza.net> writes:
Stephan Wienczny wrote:
 Hallo,
 
 I like the new way of casting except on thing: It could be a little bit
 more precise. Could we have something like this:
 
 cast(BasicType, valuetocast)
 
 A cast is IMHO nothing else than a special function, why shouldn't we
 handle it as such? The advantage of this style can be seen if you cast
 classes with members:
 
 cast(BasicType,valuetocast).foo instead of
 (cast(BasicType)valuetocast).foo

Since I have proposed this before, I concur! It's much easier to read a cast(A, B) syntax than a cast(A) B syntax. I always figured that adding syntax merely to please the parser was bollocks, and that we should be adding syntax to please the people who have to write the damn code. :-) TX -- 'Every sufficiently advanced technology is indistinguishable from magic' - Arthur C Clarke 'Every sufficiently advanced magic is indistinguishable from technology' - Tom Graves Email: Trejkaz Xaoza <trejkaz xaoza.net> Web site: http://xaoza.net/trejkaz/ Jabber ID: trejkaz jabber.xaoza.net GPG Fingerprint: 9EEB 97D7 8F7B 7977 F39F A62C B8C7 BC8B 037E EA73
Jul 02 2004
parent reply Andrew Edwards <ridimz_at yahoo.dot.com> writes:
Trejkaz Xaoza wrote:

 Stephan Wienczny wrote:
 
Hallo,

I like the new way of casting except on thing: It could be a little bit
more precise. Could we have something like this:

cast(BasicType, valuetocast)

A cast is IMHO nothing else than a special function, why shouldn't we
handle it as such? The advantage of this style can be seen if you cast
classes with members:

cast(BasicType,valuetocast).foo instead of
(cast(BasicType)valuetocast).foo

Since I have proposed this before, I concur! It's much easier to read a cast(A, B) syntax than a cast(A) B syntax. I always figured that adding syntax merely to please the parser was bollocks, and that we should be adding syntax to please the people who have to write the damn code. :-)

Find a better way to do things so you won't need to rely so heavily on casts. Use it sparingly and you won't have as many unpleasant moments. Otherwise, suck it up!
 TX
 

Jul 03 2004
parent reply Trejkaz Xaoza <trejkaz xaoza.net> writes:
In article <cc59ls$1kdm$1 digitaldaemon.com>, Andrew Edwards says...
Find a better way to do things so you won't need to rely so heavily on
casts. Use it sparingly and you won't have as many unpleasant moments.
Otherwise, suck it up!

That's a fine attitude if you're the only developer working on the project. But some of us work in these mystical offices with multiple programmers in the same place, and I doubt your slice of idealism works in this scenario. :-) TX
Jul 05 2004
parent Norbert Nemec <Norbert.Nemec gmx.de> writes:
Trejkaz Xaoza wrote:

 In article <cc59ls$1kdm$1 digitaldaemon.com>, Andrew Edwards says...
Find a better way to do things so you won't need to rely so heavily on
casts. Use it sparingly and you won't have as many unpleasant moments.
Otherwise, suck it up!

That's a fine attitude if you're the only developer working on the project. But some of us work in these mystical offices with multiple programmers in the same place, and I doubt your slice of idealism works in this scenario. :-)

Well, but in that scenario, the beauty of the casting operator should be the least of your problems...
Jul 05 2004
prev sibling next sibling parent reply Andrew Edwards <ridimz_at yahoo.dot.com> writes:
Not this again! Not every D user will be pleased with every feature of 
D. The majority of D users are comfortable with the current state of 
casts! Let it rest please! I seriously doubt the extra "()" will kill 
anyone.

Andrew

Stephan Wienczny wrote:
 Hallo,
 
 I like the new way of casting except on thing: It could be a little bit 
 more precise. Could we have something like this:
 
 cast(BasicType, valuetocast)
 
 A cast is IMHO nothing else than a special function, why shouldn't we 
 handle it as such? The advantage of this style can be seen if you cast 
 classes with members:
 
 cast(BasicType,valuetocast).foo instead of
 (cast(BasicType)valuetocast).foo
 
 You don't need the extra parenthesis arround the cast anymore.

class foo { void* data; } void main() { foo bar = new foo; bar.data = cast(void*)100; int data; data = cast(int)bar.data; // didn't need it anyway! printf("%d",data); }
 Stephan

Jul 03 2004
next sibling parent reply Trejkaz Xaoza <trejkaz xaoza.net> writes:
In article <cc58tf$1jmp$1 digitaldaemon.com>, Andrew Edwards says...
Not this again! Not every D user will be pleased with every feature of 
D. The majority of D users are comfortable with the current state of 
casts! Let it rest please! I seriously doubt the extra "()" will kill 
anyone.

<sarcasm> While we're at it, I propose we introduce a new syntax for all functions, where we use a new set of parentheses for each parameter. int result = do_your_stuff(1)(a)("fish"); I seriously doubt the extra "()" will kill anyone... </sarcasm>
Jul 05 2004
parent Derek Parnell <derek psych.ward> writes:
On Mon, 5 Jul 2004 07:13:55 +0000 (UTC), Trejkaz Xaoza wrote:

 In article <cc58tf$1jmp$1 digitaldaemon.com>, Andrew Edwards says...
Not this again! Not every D user will be pleased with every feature of 
D. The majority of D users are comfortable with the current state of 
casts! Let it rest please! I seriously doubt the extra "()" will kill 
anyone.

<sarcasm> While we're at it, I propose we introduce a new syntax for all functions, where we use a new set of parentheses for each parameter. int result = do_your_stuff(1)(a)("fish"); I seriously doubt the extra "()" will kill anyone... </sarcasm>

** XDL Compiler (c) 2009 ** [82893/1] Multiple Thyntacth Errorth ** The Thource Line Thould Thay ... (((int)(result)) (=) ((do_your_stuff)((1)(a)("fish"))(;))) -- Derek Melbourne, Australia 5/Jul/04 5:24:03 PM
Jul 05 2004
prev sibling parent Stephan Wienczny <Stephan Wienczny.de> writes:
Andrew Edwards wrote:

 Not this again! Not every D user will be pleased with every feature of
 D. The majority of D users are comfortable with the current state of
 casts! Let it rest please! I seriously doubt the extra "()" will kill
 anyone.
 
 Andrew
 
 
 class foo {
    void* data;
 }
 
 void main()
 {
    foo bar = new foo;
    bar.data = cast(void*)100;
    int data;
    data = cast(int)bar.data;  // didn't need it anyway!
    printf("%d",data);
 }
 

Sorry but exactly that is my problem! I've got code that looks like this: class Token { } class OperatorToken { int value; } Token currenttoken; ... ... switch (cast(OperatorToken)currenttoken.value) { 1: 2: default: } If I try to compile this I get this error: parser.d(594): no property 'value' for type 'Token' All I'm trying to suggest is to have something like this: CASTEXPRESSION = "cast" "(" IDENTIFIER "," IDENTIFIERLIST ")"; ^^ target type ^^ variable This should be quite easy to parse and understand. I said function in my last post because it looks like such not because it is one. Stephan
Jul 05 2004
prev sibling parent Sam McCall <tunah.d tunah.net> writes:
Stephan Wienczny wrote:

 Hallo,
 
 I like the new way of casting except on thing: It could be a little bit 
 more precise. Could we have something like this:
 
 cast(BasicType, valuetocast)
 
 A cast is IMHO nothing else than a special function, why shouldn't we 
 handle it as such?

templates. So the function syntax should look like cast!(BasicType)(value)... just like in C++. You end up with just as many parentheses ;) Sam
Jul 03 2004