www.digitalmars.com         C & C++   DMDScript  

D - Suggestion: way to get switch() value

reply Russ Lewis <spamhole-2001-07-16 deming-os.org> writes:
How about this code:
    switch(foo.bar().baz().fred().wilma().barney())
    {
    case 0..12:
        blah
        break;
    default:
        blah2
        break;
    }

It would be cool to have a keyword/automatic variable that contains the
value that was put into th switch() clause so that I don't have to call
the long string of functions again (or to have a temporary variable).

--
The Villagers are Online! villagersonline.com

.[ (the fox.(quick,brown)) jumped.over(the dog.lazy) ]
.[ (a version.of(English).(precise.more)) is(possible) ]
?[ you want.to(help(develop(it))) ]
Mar 27 2002
next sibling parent reply "Pavel Minayev" <evilone omen.ru> writes:
"Russ Lewis" <spamhole-2001-07-16 deming-os.org> wrote in message
news:3CA22EAD.6CFF0221 deming-os.org...

 How about this code:
     switch(foo.bar().baz().fred().wilma().barney())
     {
     case 0..12:
         blah
         break;
     default:
         blah2
         break;
     }

 It would be cool to have a keyword/automatic variable that contains the
 value that was put into th switch() clause so that I don't have to call
 the long string of functions again (or to have a temporary variable).

Sorta BASIC? Well, since current object is "this", we could call it "it". =) It would also be nice to have the same feature in with(): with (foo.bar.baz) { if (!okay) kill(it); } // -- OR -- switch (foo.bar.baz.okay) { case 1: kill(it); } Something I wanted to see in some language for a long time already...
Mar 27 2002
next sibling parent "OddesE" <OddesE_XYZ hotmail.com> writes:
"Pavel Minayev" <evilone omen.ru> wrote in message
news:a7te3p$2og9$1 digitaldaemon.com...
 "Russ Lewis" <spamhole-2001-07-16 deming-os.org> wrote in message
 news:3CA22EAD.6CFF0221 deming-os.org...

 How about this code:
     switch(foo.bar().baz().fred().wilma().barney())
     {
     case 0..12:
         blah
         break;
     default:
         blah2
         break;
     }

 It would be cool to have a keyword/automatic variable that contains the
 value that was put into th switch() clause so that I don't have to call
 the long string of functions again (or to have a temporary variable).

Sorta BASIC? Well, since current object is "this", we could call it "it". =) It would also be nice to have the same feature in with(): with (foo.bar.baz) { if (!okay) kill(it); } // -- OR -- switch (foo.bar.baz.okay) { case 1: kill(it); } Something I wanted to see in some language for a long time already...

Cool yes. Better than 'caseval' anyway (see my other reply)... :) -- Stijn OddesE_XYZ hotmail.com http://OddesE.cjb.net _________________________________________________ Remove _XYZ from my address when replying by mail
Mar 27 2002
prev sibling next sibling parent reply "Christophe Bouchon" <cbouchon hotmail.com> writes:
And how do you get the value of and outer switch from cases of an inner
(nested) switch ? Local(s) variable(s) is(are) back ?
Local declaration (like in for (...) in C++) should be ok:
switch(int it1 = foo.bar().baz().fred().wilma().barney())
{
case 0..12:
    switch (int it2 = foo.bar().baz().fred().wilma().casey())
    {
    case 23..41:
        do(it1 + it2);
        ...
    }
    // it2 out of scope here
    ...
}
// it1 out of scope here

By the way, I'd like something like:
    var v = expr;
declaration where "= expr" is required and v type is expr static type. This
is not an untyped variable, only an automatic typeof(expr). Sometimes, it
realy bugs me to have to search for the spelling of a type or to have to do
plenty of types copy/paste.

"Pavel Minayev" wrote:
 "Russ Lewis" <spamhole-2001-07-16 deming-os.org> wrote in message
 news:3CA22EAD.6CFF0221 deming-os.org...

 How about this code:
     switch(foo.bar().baz().fred().wilma().barney())
     {
     case 0..12:
         blah
         break;
     default:
         blah2
         break;
     }

 It would be cool to have a keyword/automatic variable that contains the
 value that was put into th switch() clause so that I don't have to call
 the long string of functions again (or to have a temporary variable).

Sorta BASIC? Well, since current object is "this", we could call it "it". =) It would also be nice to have the same feature in with(): with (foo.bar.baz) { if (!okay) kill(it); } // -- OR -- switch (foo.bar.baz.okay) { case 1: kill(it); } Something I wanted to see in some language for a long time already...

Mar 27 2002
parent "Pavel Minayev" <evilone omen.ru> writes:
"Christophe Bouchon" <cbouchon hotmail.com> wrote in message
news:a7tlvg$2si8$1 digitaldaemon.com...

 And how do you get the value of and outer switch from cases of an inner
 (nested) switch ? Local(s) variable(s) is(are) back ?

Yes. If one really need "it" from upper level, he can always catch it: switch (foo) { case 1: that = it; switch (bar) { case 2: that.does(it); } }
 Local declaration (like in for (...) in C++) should be ok:
 switch(int it1 = foo.bar().baz().fred().wilma().barney())
 {
 case 0..12:
     switch (int it2 = foo.bar().baz().fred().wilma().casey())
     {
     case 23..41:
         do(it1 + it2);
         ...
     }
     // it2 out of scope here
     ...
 }
 // it1 out of scope here

It would be great; unfortunately, D doesn't support variable declaration in switch() and with() blocks...
 By the way, I'd like something like:
     var v = expr;
 declaration where "= expr" is required and v type is expr static type.

 is not an untyped variable, only an automatic typeof(expr). Sometimes, it
 realy bugs me to have to search for the spelling of a type or to have to

 plenty of types copy/paste.

 "Pavel Minayev" wrote:
 "Russ Lewis" <spamhole-2001-07-16 deming-os.org> wrote in message
 news:3CA22EAD.6CFF0221 deming-os.org...

 How about this code:
     switch(foo.bar().baz().fred().wilma().barney())
     {
     case 0..12:
         blah
         break;
     default:
         blah2
         break;
     }

 It would be cool to have a keyword/automatic variable that contains



 value that was put into th switch() clause so that I don't have to



 the long string of functions again (or to have a temporary variable).

Sorta BASIC? Well, since current object is "this", we could call it "it". =) It would also be nice to have the same feature in with(): with (foo.bar.baz) { if (!okay) kill(it); } // -- OR -- switch (foo.bar.baz.okay) { case 1: kill(it); } Something I wanted to see in some language for a long time already...


Mar 27 2002
prev sibling parent "Stephen Fuld" <s.fuld.pleaseremove att.net> writes:
"Pavel Minayev" <evilone omen.ru> wrote in message
news:a7te3p$2og9$1 digitaldaemon.com...
 "Russ Lewis" <spamhole-2001-07-16 deming-os.org> wrote in message
 news:3CA22EAD.6CFF0221 deming-os.org...

 How about this code:
     switch(foo.bar().baz().fred().wilma().barney())
     {
     case 0..12:
         blah
         break;
     default:
         blah2
         break;
     }

 It would be cool to have a keyword/automatic variable that contains the
 value that was put into th switch() clause so that I don't have to call
 the long string of functions again (or to have a temporary variable).

Sorta BASIC? Well, since current object is "this", we could call it "it". =) It would also be nice to have the same feature in with():

snip
 Something I wanted to see in some language for a long time already...

The first computer language with pronouns! I love it. :-) But if we have trouble keeping the antecedents straight in English, I fear it might be harder in a computer language. :-( -- - Stephen Fuld e-mail address disguised to prevent spam
Mar 27 2002
prev sibling next sibling parent "OddesE" <OddesE_XYZ hotmail.com> writes:
"Russ Lewis" <spamhole-2001-07-16 deming-os.org> wrote in message
news:3CA22EAD.6CFF0221 deming-os.org...
 How about this code:
     switch(foo.bar().baz().fred().wilma().barney())
     {
     case 0..12:
         blah
         break;
     default:
         blah2
         break;
     }

 It would be cool to have a keyword/automatic variable that contains the
 value that was put into th switch() clause so that I don't have to call
 the long string of functions again (or to have a temporary variable).

 --
 The Villagers are Online! villagersonline.com

 .[ (the fox.(quick,brown)) jumped.over(the dog.lazy) ]
 .[ (a version.of(English).(precise.more)) is(possible) ]
 ?[ you want.to(help(develop(it))) ]

Do you mean something like this? switch(foo.bar().baz().fred().wilma().barney()) { case 0..12: Func (caseval); break; default: Func (caseval +2); break; } I like it, kinda like 'this' for objects. -- Stijn OddesE_XYZ hotmail.com http://OddesE.cjb.net _________________________________________________ Remove _XYZ from my address when replying by mail
Mar 27 2002
prev sibling next sibling parent reply DrWhat? <blackmarlin nospam.asean-mail.com> writes:
Russ Lewis wrote:

 How about this code:
     switch(foo.bar().baz().fred().wilma().barney())
     {
     case 0..12:
         blah
         break;
     default:
         blah2
         break;
     }
 
 It would be cool to have a keyword/automatic variable that contains the
 value that was put into th switch() clause so that I don't have to call
 the long string of functions again (or to have a temporary variable).
 

{ ... } Why not? C 2002/3/28
Mar 28 2002
parent reply "Pavel Minayev" <evilone omen.ru> writes:
"DrWhat?" <blackmarlin nospam.asean-mail.com> wrote in message
news:a7v17r$ger$3 digitaldaemon.com...

         switch( temp = foo.bar().baz().fred().wilma().barney() )
         { ... }

         Why not?

'cause you have to declare temp outside of switch(), and it will thus interfere with other switch blocks out there. If only we could declare variables like in for: switch (int temp = foo.bar.baz) But even then, why not let the compiler do it, since it's rather common?
Mar 28 2002
parent reply DrWhat? <blackmarlin nospam.asean-mail.com> writes:
Pavel Minayev wrote:

 "DrWhat?" <blackmarlin nospam.asean-mail.com> wrote in message
 news:a7v17r$ger$3 digitaldaemon.com...
 
         switch( temp = foo.bar().baz().fred().wilma().barney() )
         { ... }

         Why not?

'cause you have to declare temp outside of switch(), and it will thus interfere with other switch blocks out there. If only we could declare variables like in for: switch (int temp = foo.bar.baz)

sorry as I did not know the type foo returns I ommitted specifying the variable define (damn nitt pickers :-)) - what I ment was effectively switch( typeReturnedByFoo temp = foo.bar .... and so on
 But even then, why not let the compiler do it, since it's
 rather common?

Because that would probably require either another keyword to access. We could use a variable with local scope to the switch block, or auto type define on a = ... the first would require a lot of compiler support and the second can be tricky to implement. So why bother when it is easy to declare a tempory variable, after all in object oriented programming procedures (methods) are normally quite short and having a few extra tempory references should not be an enormous problem.
Mar 28 2002
parent reply "Pavel Minayev" <evilone omen.ru> writes:
"DrWhat?" <blackmarlin nospam.asean-mail.com> wrote in message
news:a7v78o$k4t$1 digitaldaemon.com...

 Because that would probably require either another keyword to access.

Yes, so what? Another keyword is not a big price for this, I think.
 We could use a variable with local scope to the switch block,  or auto
 type define on a = ... the first would require a lot of compiler support
 and the second can be tricky to implement.  So why bother when it is easy
 to declare a tempory variable,  after all in object oriented programming
 procedures (methods) are normally quite short and having a few extra
 tempory references should not be an enormous problem.

You mean declaring temporary outside of switch block? Then why bother declaring counter inside for? I believe that variables should be as local as possible, so, for the value of switch expression, it should be local to switch - whatever way it is done.
Mar 28 2002
parent reply DrWhat? <blackmarlin nospam.asean-mail.com> writes:
Pavel Minayev wrote:
 
 You mean declaring temporary outside of switch block? Then
 why bother declaring counter inside for?
 I believe that variables should be as local as possible, so, for
 the value of switch expression, it should be local to switch -
 whatever way it is done.

Well so long as I can increase the scope of the variables to that of the entire procedure I have no problem with allowing variables to be even more local - I really dislike the way VHDL forces you to use locals to for loops which dissappear after the loops exits, stopping you reading the final value. However the D syntax seems to make this senario highly improbable. I would be quite happy with allowing variables to be scoped within a block, so long as we do not end up allowing (as some languages do) ... int x; switch( int x = foo() ) { ... } After all I am not writing the compiler. C 2002/3/28
Mar 28 2002
parent "Pavel Minayev" <evilone omen.ru> writes:
"DrWhat?" <blackmarlin nospam.asean-mail.com> wrote in message
news:a7vnhi$th8$1 digitaldaemon.com...

 I would be quite happy with allowing variables to be scoped within a

 so long as we do not end up allowing (as some languages do) ...
         int x;
         switch( int x = foo() ) { ... }

This is exactly how D works. You can have a variable local to block rather than function, but you cannot have two locals with the same name at once. Now if we get that in-place declaration for switch(), I guess the problem is solved.
Mar 28 2002
prev sibling next sibling parent reply Russ Lewis <spamhole-2001-07-16 deming-os.org> writes:
BOGGLE...here's a weird idea.  What if you could declare a named switch,
kind of like declaring a variable?



    int myVar switch(foo.bar().baz().fred().wilma().barney())
    {
    case 0..12:
        DoStuff(myVar); break;
    default:
        DoOtherStuff(myVar);
    }

The scope of the variable would be just the switch block.  Or what about
this syntax:

    switch(foo.bar().baz().fred().wilma().barney()) as in myVar {...}

Thoughts?

--
The Villagers are Online! villagersonline.com

.[ (the fox.(quick,brown)) jumped.over(the dog.lazy) ]
.[ (a version.of(English).(precise.more)) is(possible) ]
?[ you want.to(help(develop(it))) ]
Mar 28 2002
next sibling parent Russ Lewis <spamhole-2001-07-16 deming-os.org> writes:
Russ Lewis wrote:

     switch(foo.bar().baz().fred().wilma().barney()) as in myVar {...}

I meant "int", not "in": switch(...) as int myVar {...} -- The Villagers are Online! villagersonline.com .[ (the fox.(quick,brown)) jumped.over(the dog.lazy) ] .[ (a version.of(English).(precise.more)) is(possible) ] ?[ you want.to(help(develop(it))) ]
Mar 28 2002
prev sibling next sibling parent "Pavel Minayev" <evilone omen.ru> writes:
"Russ Lewis" <spamhole-2001-07-16 deming-os.org> wrote in message
news:3CA335AA.E367FCB4 deming-os.org...

 BOGGLE...here's a weird idea.  What if you could declare a named switch,
 kind of like declaring a variable?

     int myVar switch(foo.bar().baz().fred().wilma().barney())

 The scope of the variable would be just the switch block.  Or what about
 this syntax:

     switch(foo.bar().baz().fred().wilma().barney()) as in myVar {...}

 Thoughts?

Then, it'd be better to use for-syntax: switch (int myVar = foo.bar().baz()...)
Mar 28 2002
prev sibling next sibling parent reply Ben Cohen <bc skygate.co.uk> writes:
On Thu, 28 Mar 2002 15:24:26 +0000, Russ Lewis wrote:

 BOGGLE...here's a weird idea.  What if you could declare a named switch,
 kind of like declaring a variable?
 
     int myVar switch(foo.bar().baz().fred().wilma().barney()) { case
     0..12:
         DoStuff(myVar); break;
     default:
         DoOtherStuff(myVar);
     }

I thought of an idea like this. The next step would be to say that rather than doing "continue/goto case 12" you could simply set myVar to 12 and "continue". If you wanted to break you could do so as normal. This gives you a kind of state-machine switch loop. Nice and very powerful but perhaps a bit dangerous:-) Ben.
Mar 28 2002
parent Russ Lewis <spamhole-2001-07-16 deming-os.org> writes:
Ben Cohen wrote:

 This gives you a kind of state-machine switch loop.  Nice and very
 powerful but perhaps a bit dangerous:-)

Yeah, there's a certain coolness factor to it...but seems like it would be confusing. Putting a switch inside a while loop would probably be more readable :) -- The Villagers are Online! villagersonline.com .[ (the fox.(quick,brown)) jumped.over(the dog.lazy) ] .[ (a version.of(English).(precise.more)) is(possible) ] ?[ you want.to(help(develop(it))) ]
Mar 28 2002
prev sibling parent reply "Sean L. Palmer" <spalmer iname.com> writes:
The only thing this thread does for me is make me want the old Pascal 'with'
statement.  Even that had some flaws.  I wasn't aware that D had a with
statement.  I should go look that up.

I just can't see why you can't merely do this:

int myVar = foo.bar().baz().fred().wilma().barney();
switch (myVar)
{
     case 0..12:
         DoStuff(myVar); break;
     default:
         DoOtherStuff(myVar);
}

I don't see what you're gaining in the below.  Also, both the syntaxes below
just look wonky to me.

Some way to declare the variable inside the switch expression ala 'for'
loops would be great.  Same with 'while' and 'if', although it's hard to
combine a declaration with a comparison to produce the needed boolean.

if (char c = GetChar() < ' ')
  TranslateWhiteSpace(c);

Sean

"Russ Lewis" <spamhole-2001-07-16 deming-os.org> wrote in message
news:3CA335AA.E367FCB4 deming-os.org...
 BOGGLE...here's a weird idea.  What if you could declare a named switch,
 kind of like declaring a variable?



     int myVar switch(foo.bar().baz().fred().wilma().barney())
     {
     case 0..12:
         DoStuff(myVar); break;
     default:
         DoOtherStuff(myVar);
     }

 The scope of the variable would be just the switch block.  Or what about
 this syntax:

     switch(foo.bar().baz().fred().wilma().barney()) as in myVar {...}

 Thoughts?

Mar 28 2002
next sibling parent "Pavel Minayev" <evilone omen.ru> writes:
"Sean L. Palmer" <spalmer iname.com> wrote in message
news:a7vsn9$1076$1 digitaldaemon.com...

 I just can't see why you can't merely do this:

 int myVar = foo.bar().baz().fred().wilma().barney();
 switch (myVar)

Because myVar will now be clattering the namespace, while it's only needed _inside_ the switch (like in most cases the loop counter is only needed inside the loop). Of course, an additional pair of braces could be added... but it is a hack.
Mar 28 2002
prev sibling parent reply "Walter" <walter digitalmars.com> writes:
"Sean L. Palmer" <spalmer iname.com> wrote in message
news:a7vsn9$1076$1 digitaldaemon.com...
 The only thing this thread does for me is make me want the old Pascal

 statement.  Even that had some flaws.  I wasn't aware that D had a with
 statement.  I should go look that up.

I'll save you the trouble <g>. D has a with statement. To tell the truth, I'm not that sure it's a good idea, but it is there.
Apr 23 2002
parent reply "Pavel Minayev" <evilone omen.ru> writes:
"Walter" <walter digitalmars.com> wrote in message
news:aa336i$274n$1 digitaldaemon.com...

 I'll save you the trouble <g>. D has a with statement. To tell the truth,
 I'm not that sure it's a good idea, but it is there.

It is a good idea. I even think it would be better if it worked with structures too, especially when filling large WinAPI structs (WNDCLASS, LOGFONT etc).
Apr 23 2002
parent "Walter" <walter digitalmars.com> writes:
"Pavel Minayev" <evilone omen.ru> wrote in message
news:aa3lsj$2rhv$1 digitaldaemon.com...
 "Walter" <walter digitalmars.com> wrote in message
 news:aa336i$274n$1 digitaldaemon.com...
 I'll save you the trouble <g>. D has a with statement. To tell the


 I'm not that sure it's a good idea, but it is there.

structures too, especially when filling large WinAPI structs (WNDCLASS, LOGFONT etc).

That's a good idea. -Walter
Apr 23 2002
prev sibling parent reply "Richard Krehbiel" <rich kastle.com> writes:
"Russ Lewis" <spamhole-2001-07-16 deming-os.org> wrote in message
news:3CA22EAD.6CFF0221 deming-os.org...
 How about this code:
     switch(foo.bar().baz().fred().wilma().barney())
     {
     case 0..12:
         blah
         break;
     default:
         blah2
         break;
     }

 It would be cool to have a keyword/automatic variable that contains the
 value that was put into th switch() clause so that I don't have to call
 the long string of functions again (or to have a temporary variable).

I hate to be a nay-sayer for interesting ideas... but some things are too easily done to be worth simplifying further. I can't think of any problem with saying: int c = foo.bar().baz().fred().wilma().barney(); switch(c) { But one other suggestion, that of putting the declaration in the switch, as in "switch(int c = foo.bar().baz().fred().wilma().barney())", suggests the notion that a declaration with an initializer is an expression with a value. That might be worth thinking about. -- Richard Krehbiel, Arlington, VA, USA rich kastle.com (work) or krehbiel3 comcast.net (personal)
Mar 28 2002
parent reply "Sean L. Palmer" <spalmer iname.com> writes:
 But one other suggestion, that of putting the declaration in the switch,

 in "switch(int c = foo.bar().baz().fred().wilma().barney())", suggests the
 notion that a declaration with an initializer is an expression with a

 That might be worth thinking about.

Yes I like that idea... but most of the time you wouldn't want to declare variables inside a complex expression. I suppose their scope would usually just be the scope of the statement. Sean
Mar 28 2002
parent "Richard Krehbiel" <rich kastle.com> writes:
"Sean L. Palmer" <spalmer iname.com> wrote in message
news:a7vt2l$10ac$1 digitaldaemon.com...
 But one other suggestion, that of putting the declaration in the switch,

 in "switch(int c = foo.bar().baz().fred().wilma().barney())", suggests


 notion that a declaration with an initializer is an expression with a

 That might be worth thinking about.

Yes I like that idea... but most of the time you wouldn't want to declare variables inside a complex expression. I suppose their scope would usually just be the scope of the statement.

I think I'd prefer that it have the same scope as if the declaration came in the immediately preceeding statement. { int a; // ... b not visible here... a = (int b = c * 5); // b visible here, and extends to closing brace. } -- Richard Krehbiel, Arlington, VA, USA rich kastle.com (work) or krehbiel3 comcast.net (personal)
Mar 28 2002