Re: 'switch-expression' and the 'type guard' unary operator demo

From: Date: Mon, 21 Oct 2019 20:42:25 +0000
Subject: Re: 'switch-expression' and the 'type guard' unary operator demo
References: 1 2 3 4 5 6 7  Groups: php.internals 
Request: Send a blank email to internals+get-107617@lists.php.net to get a copy of this message
> On Oct 20, 2019, at 11:11 PM, Kosit Supanyo <webdevxp.com@gmail.com> wrote: > Ok, so let's look at a scenario. Will we be able to do this in the future, if we decide to > allow multiple expressions to result in a final value? > $y = switch ($x) { > case 1 => $arr = get_array(), array_pop($arr), > case -1 => $arr = get_array(), array_unshift($arr), > default => null, > }; > > As I told you in previous reply, I'm working on block-expressions as well. So if PHP has > block-expressions you can do the same this way: > > $y = switch ($x) { > case 1 => eval { > $arr = get_array(); > give array_pop($arr); > }, > case -1 => eval { > $arr = get_array(); > give array_shift($arr); > }, > default => null, > }; I know you said you were working on block expressions, but I did not view that as the best solution to the question I asked although I see that is how you envision it. > Which is obviously cleaner and more powerful. That is arguable. I find the solution you proposed above that uses eval() to be very "busy" visually, and needlessly complex/ You could instead simply assume that case statements supports multiple lines with the last providing the value, and thus not requiring the use of an explicit eval(). But to do that would require semi-colon line terminators, I think. Note I also think eval() would also be a nice to have, but what would be even nicer is if the inline switch did not require use of eval() to support multiple lines per case. #fwiw -Mike

« previous php.internals (#107617) next »