Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:107595 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 22425 invoked from network); 21 Oct 2019 01:25:39 -0000 Received: from unknown (HELO php-smtp3.php.net) (208.43.231.12) by 76.75.200.58 with SMTP; 21 Oct 2019 01:25:39 -0000 Received: from php-smtp3.php.net (localhost [127.0.0.1]) by php-smtp3.php.net (Postfix) with ESMTP id 6E4382CFC3A for ; Sun, 20 Oct 2019 16:09:10 -0700 (PDT) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on php-smtp3.php.net X-Spam-Level: X-Spam-Status: No, score=-2.0 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FROM,HTML_MESSAGE,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE autolearn=no autolearn_force=no version=3.4.2 X-Spam-ASN: AS3215 2.6.0.0/16 X-Spam-Virus: No Received: from mail-ot1-x32e.google.com (mail-ot1-x32e.google.com [IPv6:2607:f8b0:4864:20::32e]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by php-smtp3.php.net (Postfix) with ESMTPS for ; Sun, 20 Oct 2019 16:09:09 -0700 (PDT) Received: by mail-ot1-x32e.google.com with SMTP id 21so9401208otj.11 for ; Sun, 20 Oct 2019 16:09:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=ccqWYZSc18f+xpBn8h9+TQoyd58imLxRR6u/mtxh03M=; b=Jcd0YhDXd0LNvm5JDRIbeFGLnlkYE2jLi8MRzdK7Nbpy0d957f2IU5wmkVnTZQE4Qv 7gXuJkhPG/UnsSuuTHq20UL46413WhcLKOaCDOMvadZ9Mtbr59zZqJ0x6fhQ0iXpJoz5 rC2eO4hYrde1cdYxhqve2cuuzaxArYDQ+rVSp56rke23G7jwh0SQgkeCT1kE0a077Z4u pt7GI2IV3+msFayzIGfmoiRcsGbnCktWKZk7ZSLnZfo2ozZvPZ5gbil262+LIIZE+meA PhEl+tbOG+0ZuGnCl8aQmwA6YeOPGerE9jAOX2f1w7AvZjSh++Ij8m/ZHml4t4lkoSqm +f1Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=ccqWYZSc18f+xpBn8h9+TQoyd58imLxRR6u/mtxh03M=; b=K5a5FYnwiWeGiBFGHQN3ogkVwkSnN4ECl0xI2GhKNyS2AxWXDsDkyUUEUhT7Hw+OxU Ur7xcZg+G0tNaDPvOTb4g4h1nI3bfhcIVLIaMFIvobH59JWG5Xo52JF83/V+H69oMNFK FrXo9Rt6abayGtEHchIG5Kb47u9NIKsdIwaT9PTCAsgChPPRQOUJSvBd3zoLT6B5aR7T 8q9Y3Rr8lSMTqTEdzchZUfcZozqPRVJBegH2acwCCO9k8fhAW/mu9+Fze8Hl3CYEUd5t l0euLTMBQZXW25goUO9REGilNGaJ/ZREvpMVDqnpDNB/MNGkBOFNTvoOH6II9/cc5MtW 6Ibg== X-Gm-Message-State: APjAAAW9IpVwyfZZdKFiz6lhRJYW5n2dPSCsVNGs18+cAmpRWreJJ20T fIoPkAFnAINpaUhJ8tIw2oau661TRup9Exa+yn7yq+wL X-Google-Smtp-Source: APXvYqxLjAHK4KTtXeRmMC8pQhTLl/HYXGGp+UjFNTgQAo1mAoC0x/nX5ofmPqWy+XK+qdMb3LLygyn9WbtrTe7/0B0= X-Received: by 2002:a9d:6b82:: with SMTP id b2mr15797752otq.56.1571612949226; Sun, 20 Oct 2019 16:09:09 -0700 (PDT) MIME-Version: 1.0 References: <3D85D31D-5DDD-42AA-B465-6ADBE72D952A@newclarity.net> In-Reply-To: <3D85D31D-5DDD-42AA-B465-6ADBE72D952A@newclarity.net> Date: Mon, 21 Oct 2019 06:08:59 +0700 Message-ID: To: Mike Schinkel Cc: PHP internals Content-Type: multipart/alternative; boundary="000000000000902ae005955fa72c" X-Envelope-From: Subject: Re: [PHP-DEV] 'switch-expression' and the 'type guard' unary operator demo From: webdevxp.com@gmail.com (Kosit Supanyo) --000000000000902ae005955fa72c Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Hi Mike In a brace-enclosed block statements are separated by semi-colons, not > commas. Yes because they are statements, that's the point which I've stated many times. And if your argument is that there's no place in PHP that uses commas within curly braces, you're wrong. PHP already has one. use X\Y\Z\{ A, B, C, }; The reason to choose what to be a separator is not about the container but rather its items and semantics associated with them. And there's many languages out there using commas to separate expressions within curly braces if you did do some researches you might not think they are inconsistent at all. In fact I'd already gave you some examples in previous reply (C#, Rust). We can either limit a brace-enclosed expression to containing only simple > expressions =E2=80=94 which as I said seems too limiting =E2=80=94 or we = can enable the > future *potential *for multi-line statements in a block-enclosed > expression. No one is limiting anything. I'm working on block expressions too. One of them is `eval` block which would be like: $z =3D eval { $x =3D doSomething1(); $y =3D doSomething2(); if ($x >=3D 0 && $y >=3D 0) { give $x + $y; } give null; // can be omitted }; If you think that anything inside curly braces must be separated by semicolon, you are the one that limit yourself. As I envision, it should break `for`. And since the fact the call frame is > already pushed does not stop a `throw` from working I do not see why it > should stop a `break` from working. > That's because `throw` do different thing at the engine level not just jumping things like `break`, `continue` or `goto`. BTW, when I code I use `break` wherever possible instead of `throw`[1-6], > and only ever use `throw` and `try...catch` when I am forced to. So > supporting throw but not break here would require an additional comparis= on > after the `switch` expression for people who like me avoid `throw`, which > is not ideal. If `break` is allowed in switch-expressions and its behavior is to break outer loop, don't you think it could be an another inconsistency or confusion? Because I'm 100% sure when people see `break` within `switch` they'll all think it should break `switch` not something else. Regards On Mon, Oct 21, 2019 at 4:12 AM Mike Schinkel wrote: > Hi Kosit, > > Thank you for your reply. > > On Oct 20, 2019, at 3:54 AM, Kosit Supanyo wrote= : > > Hi Mike > > Is there a strong reason to change to using fat arrows and colons? > > The reason is statement/expression semantic. > > > Okay, so you chose commas and fat arrows because of perceived consistency= . > I can buy that. > > However, I think by being consistent with one set of PHP syntax you ended > up being inconsistent with another set; the brace-enclosed block. In a > brace-enclosed block statements are separated by semi-colons, not commas. > And given your use of `case` we could argue that the `case`s are > sub-statements of the switch. > > So I think both of our arguments as suspect here you are actually > introducing a new syntactical structure with this switch expression that > has no current analogue in PHP. It is unlike anything else that comes > before it, and so I think it would be a mistake to be consistent in some > ways but not in others. Better to introduce rules that are specific to th= is > structure that to justify the rules based on only partial consistency. > > Basically you are introducing the following syntax where the entirety is > an expression: > > [()] { > } > > And we should explicitly consider what are the best rules to use for this > rather than allow the rules to be defined based on consistency with > something that is not like this new structure and thus potentially paint = us > into a corner in the future simple because we did not evaluate it fully. > > The question I think we should consider here is, what makes for a valid > ? Is it a single expression, or can be be > multiple expressions with a delimiter where the last expression produces > the resultant value? Or can it even be multiple statements, the last of > which produces the resultant value? > > > We can limit to single expressions, but that seems, well, limiting. If w= e > instead leave open the option for multiple statements =E2=80=94 which a s= emi-colon > would not conflict with =E2=80=94 then we leave open the future option to= support > multiple statements in a block-enclosed expression. But if we choose a > comma we can never go down that path. Given this, *I think using comma > paints us into a corner that using semi-colon would not.* > > > As for fat arrows vs. colons, that too feels like misapplication of > consistency. A `case` is more like a ternary operator than an array > initializer, and is much more like a switch statement. I fear that use of= a > fat arrow will also paint us into a corner, but I cannot think of a > specific example yet, it is just a fear based on my gut feeling. Using a > colon would likely not paint us into a corner because it has already been > used in effectively exactly the same context, e.g. the `case` statement f= or > a `switch`. > > > So the question is, do we want to be consistent with an expression but > inconsistent with brace-enclosed blocks, *or do we want to recognize that > this is a new type of element and design the best rules we can for this n= ew > type of element?* > > > We can either limit a brace-enclosed expression to containing only simple > expressions =E2=80=94 which as I said seems too limiting =E2=80=94 or we = can enable the > future *potential *for multi-line statements in a block-enclosed > expression. > > > And that is my 200 cents on the matter. :-) > > > > What about break? > > > Let's see this example. > > for ($i =3D 0; $i < 100; $i++) { > $x =3D getX($i); > doSomething( > $i, > $x, > switch ($x) { > case 0 =3D> 'ZERO', > case 1 =3D> 'ONE', > case 2 =3D> 'TWO', > case 3 =3D> 'THREE', > default =3D> break, > }, > ); > } > > This should break `switch` or `for`? And if it breaks `switch` NULL shoul= d > be returned but this will be the same as omitting `default`. And if > it breaks `for`, call to `doSomething()` will be incomplete because a cal= l > frame was already pushed to VM stack. As I know ongoing function > calls can only be aborted by exceptions (and `return` should not be allow= ed > either, I need to fix that) so expressions must give something or throw. > > > As I envision, it should break `for`. And since the fact the call frame > is already pushed does not stop a `throw` from working I do not see why i= t > should stop a `break` from working. > > > And if for some reason that is too complicated, I do not see any reason > why it could not throw an error in that context, because the refactored > solution is trivial, and I would say, preferred for its readability anywa= y: > > > for ($i =3D 0; $i < 100; $i++) { > $x =3D getX($i); > $y =3D switch ($x) { > case 0 =3D> 'ZERO', > case 1 =3D> 'ONE', > case 2 =3D> 'TWO', > case 3 =3D> 'THREE', > default =3D> break, > }, > doSomething( $I, $x, $y ); > } > > > BTW, when I code I use `break` wherever possible instead of `throw`[1-6], > and only ever use `throw` and `try...catch` when I am forced to. So > supporting throw but not break here would require an additional comparis= on > after the `switch` expression for people who like me avoid `throw`, which > is not ideal. > > > -Mike > [1] http://www.lighterra.com/papers/exceptionsharmful/ > [2] https://sidburn.github.io/blog/2016/03/25/exceptions-are-evil > [3] http://xahlee.info/comp/why_i_hate_exceptions.html > [4] https://www.joelonsoftware.com/2003/10/13/13/ > [5] https://mortoray.com/2012/04/02/everything-wrong-with-exceptions/ > [6] https://www.atlassian.com/blog/archives/exceptions_are_bad > > --000000000000902ae005955fa72c--