On Wed, Aug 13, 2014 at 2:52 PM, Rowan Collins <rowan.coll...@gmail.com>
wrote:

> Ferenc Kovacs wrote (on 13/08/2014):
>
>> not sure what do you mean here, multiple default cases can be reached the
>>
>> same way as any other duplicated case branch(as shown in my snippet in the
>> mail you replied to).
>>
>
> If you're talking about http://3v4l.org/eZdPU then those duplicates are
> definitely being silently ignored. Remember that the switch statement is
> just a glorified goto, so execution starts at the first matching case and
> then carries on until you hit a "break" or the end of the switch block.
>

oh, I thought Andrea was referring to ignoring the code from the case, not
the "label" of the case block.
yeah, that is how it should work except in the original bugreport, where we
jump to the last matching case but only when there is no previous
non-default matching case.


>
> In that example, it finds the first "case 'foo':", does a goto, and at
> that point all the other labels are completely irrelevant; the 4 echo
> statements are executed in order as though the other labels didn't exist.
>
> If you feed it something other than 'foo', then the behaviour being
> discussed kicks in: for whatever reason, the *last* default label is
> selected as the target of the goto, so only the last echo is executed:
> http://3v4l.org/TfYiQ
>
> Interestingly, HHVM apparently behaves the same on that example, although
> others mentioned it selecting the first rather than last default label.


yeah, I noticed this and mentioned in my comment to the bugreport.


>
>
>  not sure about the multiple defaults, but I'm fairly sure that there are
>> code out there which uses switch blocks as state-machines, and changes the
>> switch variable in the case blocks, and have duplicated case blocks for
>> executing code after the switch variable reached it's final form.
>>
>
> Multiple cases do not make any difference here; the label to jump to is
> selected only once, so if you want to change the state and jump to a
> different label, you have to wrap the switch in a loop, and evaluate it
> again. If you specify multiple labels matching the same state, the second
> one can never be reached. http://3v4l.org/i3746


I think you misunderstood that part, I was thinking about a state machine
like this:

   1. function doStuff(){
   2.   switch($state){
   3.     case OPENDOOR:
   4.       if(!opendoor()){
   5.          break;
   6.       }
   7.       $state = SITDOWN;
   8.     case SITDOWN:
   9.       if(!sitdown()){
   10.          break;
   11.       }
   12.       $state = SIPWHISKEY;
   13.     case SIPWHISKEY:
   14.       sipwhiskey();
   15.   }
   16. }


where you modify the switch variable in one case and fall through into
another.
as I mentioned I don't have a reasonable use-case for multiple defaults,
but I can see some for multiple case labels in general, and I don't think
that it is better to have different behavior for case and default in this
regard.

-- 
Ferenc Kovács
@Tyr43l - http://tyrael.hu

Reply via email to