Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:76482 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 36246 invoked from network); 13 Aug 2014 13:10:38 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 13 Aug 2014 13:10:38 -0000 Authentication-Results: pb1.pair.com smtp.mail=tyra3l@gmail.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=tyra3l@gmail.com; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.192.52 as permitted sender) X-PHP-List-Original-Sender: tyra3l@gmail.com X-Host-Fingerprint: 209.85.192.52 mail-qg0-f52.google.com Received: from [209.85.192.52] ([209.85.192.52:64179] helo=mail-qg0-f52.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 93/94-11625-CC36BE35 for ; Wed, 13 Aug 2014 09:10:38 -0400 Received: by mail-qg0-f52.google.com with SMTP id f51so10443978qge.25 for ; Wed, 13 Aug 2014 06:11:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; bh=bZU1TP0H4vv9iXU/WkrmS2jD/vxHL5lIBm11Pu2JzbQ=; b=Us5wdM59pgx0HsWUWKFOEql2W3O4lRf9Xr4X7b0ydZSfuzp1WZ4YfayORmliVlB1gS a8udN1VxysKfnJKPZ43r0ATPmS/dQdlnwqtaVgZCmr+Tf07swjiWxxT7Y7rgop1hsLWM Tc6BsfxdPeroOzbbRyrM9bKgVPUK9hUUGRzTZZ6LHeewI77x7VWAFAa7laFQqbtuYkcE 6FZh2BGabetJ9Xu6vllykuYIH8mRIBxyY26qMOGdiIUv6vHQKMTUR7447LPhC9EKzgOy fdWEh2TjIt8VcefvSy/mxKq5JEbgKox7CtehAjaGTnVu/iq+RBDrh27pDBpsT7ExeCTm oMwA== MIME-Version: 1.0 X-Received: by 10.140.102.117 with SMTP id v108mr6011939qge.93.1407935479229; Wed, 13 Aug 2014 06:11:19 -0700 (PDT) Received: by 10.140.102.111 with HTTP; Wed, 13 Aug 2014 06:11:19 -0700 (PDT) In-Reply-To: <53EB5F8E.5050607@gmail.com> References: <942D2AF6-BDC2-4214-BD58-4B5945C28CE3@ajf.me> <53EB5F8E.5050607@gmail.com> Date: Wed, 13 Aug 2014 15:11:19 +0200 Message-ID: To: Rowan Collins Cc: PHP Internals Content-Type: multipart/alternative; boundary=001a11c16ec41b35bc050082864f Subject: Re: [PHP-DEV] [RFC] Disallow multiple default blocks in a single switch statement From: tyra3l@gmail.com (Ferenc Kovacs) --001a11c16ec41b35bc050082864f Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable On Wed, Aug 13, 2014 at 2:52 PM, Rowan Collins wrote: > Ferenc Kovacs wrote (on 13/08/2014): > >> not sure what do you mean here, multiple default cases can be reached th= e >> >> same way as any other duplicated case branch(as shown in my snippet in t= he >> 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 t= he >> 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 =3D SITDOWN; 8. case SITDOWN: 9. if(!sitdown()){ 10. break; 11. } 12. $state =3D 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. --=20 Ferenc Kov=C3=A1cs @Tyr43l - http://tyrael.hu --001a11c16ec41b35bc050082864f--