March 28, 2002 Re: Suggestion: way to get switch() value | ||||
---|---|---|---|---|
| ||||
Posted in reply to Russ Lewis | 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))) ] |
March 28, 2002 TYPO AGAIN | ||||
---|---|---|---|---|
| ||||
Posted in reply to Russ Lewis | 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))) ] |
March 28, 2002 Re: Suggestion: way to get switch() value | ||||
---|---|---|---|---|
| ||||
Posted in reply to Russ Lewis | "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()...) |
March 28, 2002 Re: Suggestion: way to get switch() value | ||||
---|---|---|---|---|
| ||||
Posted in reply to DrWhat? | "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. |
March 28, 2002 Re: Suggestion: way to get switch() value | ||||
---|---|---|---|---|
| ||||
Posted in reply to Russ Lewis | 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.
|
March 28, 2002 Re: Suggestion: way to get switch() value | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ben Cohen | 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))) ] |
March 28, 2002 Re: Suggestion: way to get switch() value | ||||
---|---|---|---|---|
| ||||
Posted in reply to Russ Lewis | "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) |
March 28, 2002 Re: Suggestion: way to get switch() value | ||||
---|---|---|---|---|
| ||||
Posted in reply to Pavel Minayev | 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
|
March 28, 2002 Re: Suggestion: way to get switch() value | ||||
---|---|---|---|---|
| ||||
Posted in reply to DrWhat? | "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 block, > 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. |
March 28, 2002 Re: Suggestion: way to get switch() value | ||||
---|---|---|---|---|
| ||||
Posted in reply to Russ Lewis | 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? |
Copyright © 1999-2021 by the D Language Foundation