Re: Little switch improvement: get the used variable

From: Date: Thu, 25 Sep 2014 09:10:15 +0000
Subject: Re: Little switch improvement: get the used variable
References: 1 2 3 4 5  Groups: php.internals 
Request: Send a blank email to internals+get-77616@lists.php.net to get a copy of this message
Rowan Collins wrote (on 24/09/2014):
On 24/09/2014 22:33, Andrea Faulds wrote:
On 24 Sep 2014, at 22:17, Rowan Collins <rowan.collins@gmail.com> wrote:
Perhaps rather than a magic function or constant, though, the switch statement could be extended with an "as" argument, which would store the evaluated expression into a normal variable, allowing nesting, and easier optimisation of the engine where the feature isn't used. Thus you could write this: switch( some_expression() as $switch_value ){ case 1:
    do_something();
break; //... default:
    throw new Exception('Undefined input: ' . $switch_value);
    break;
}
Incredibly, some brave soul has gone back in time and already implemented this, when none of us was looking!
     switch($switch_value = some_expression()) {
         ...
     }
Heh, now I feel like a fool for not thinking of that. Treating assigments as expressions just isn't something that jumps to mind, I guess!
Anyone have any thoughts on the use(operator) part, though? Am I missing something equally obvious there? Or just, it doesn't interest people much as an idea? -- Rowan Collins [IMSoP]

« previous php.internals (#77616) next »