Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:109601 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 19241 invoked from network); 12 Apr 2020 23:34:41 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 12 Apr 2020 23:34:41 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id CE3941804CE for ; Sun, 12 Apr 2020 15:03:57 -0700 (PDT) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=0.6 required=5.0 tests=BAYES_50,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=3.4.2 X-Spam-ASN: AS15169 209.85.128.0/17 X-Spam-Virus: No X-Envelope-From: Received: from mail-wm1-f53.google.com (mail-wm1-f53.google.com [209.85.128.53]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Sun, 12 Apr 2020 15:03:57 -0700 (PDT) Received: by mail-wm1-f53.google.com with SMTP id a201so7964587wme.1 for ; Sun, 12 Apr 2020 15:03:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=subject:to:references:from:message-id:date:user-agent:mime-version :in-reply-to:content-transfer-encoding:content-language; bh=8wy+PpvdsdY9omt5qmneBizFBRGi3UKlrERkXakZnkY=; b=Q+oH5vowbr6W/0ynTBG2bw44He12INc6Sqs8a/uKMeo8pUCuhaFC+4unvJsdh3Equg H4bXPQYIievK49mdnVdwdyOoh1OYhl7Jco4r7mOvpXxiwyqn6L9DFcPtrCxjEQ3JFpEw t66p43GfC0Rl3oyV4m8qXQ0dsEOYn1WAI+BCEgEfO/ydq4A8D1GwjAkbd/uZXNVp8NAS m9lKGr1ZxIhiARV6ZUERwRhbMcCgIBG8HI8hZxQTXGhZjy2E9Pr2p1H2EbVkPGbGwRsN 3kBlr4W+eiGFuZ//cFKfeqJGlBtPPzBhCFZb2PfbuVlvus2pogTBHMs8AuwqFbWJlPmK 6H6g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-transfer-encoding :content-language; bh=8wy+PpvdsdY9omt5qmneBizFBRGi3UKlrERkXakZnkY=; b=lwYmWKn+BcqLFRk2jE0BcnhddPwVcKzwhcAySxNHZF7rdJKrhuvCCh5KM/fjPbSk+9 1GurnZNP1p/TPnLXoh7GMSg9J3aGyLGBnDZydtFbmIG9a5lPJmT+QRFgH1oL5Q/kvPmf TsPlfT5qnF5DIEKIBJngULtBMyXvUhQUvUSqz1DwUNgivcY4x+UNQQtiF2AZmVwAW00G 9NxngAh55SJG0T0+lwij/xguXseXqVxsC2cxaVXjCOez/jev+Xtehaoayaj1VKGc+g80 9v58MisMdoU/ZpNU+pIuQ21qL0F8sUbbh7SAs4b+xK0MXKMsTMXX/g8JWIctA6YiA1dn BH7Q== X-Gm-Message-State: AGi0PuZ5NpSosps+wCJ5AL8ojGbkLaNPh2Sxol9OWJLPYKNPY4wxCuhz DW3bQV+3vjG9V6X8lR1QYI2FrsAD X-Google-Smtp-Source: APiQypIe8PslBqvBZttnl+byhVyBaiaM237B4rzpldFjnjMMhoqV5f6epPrdUTDKCv9+cbRhFWBaMw== X-Received: by 2002:a7b:cb59:: with SMTP id v25mr16761200wmj.13.1586729033958; Sun, 12 Apr 2020 15:03:53 -0700 (PDT) Received: from [192.168.0.14] (cpc84253-brig22-2-0-cust114.3-3.cable.virginm.net. [81.108.141.115]) by smtp.googlemail.com with ESMTPSA id h4sm12643179wrv.91.2020.04.12.15.03.53 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sun, 12 Apr 2020 15:03:53 -0700 (PDT) To: internals@lists.php.net References: Message-ID: <6aebe21d-2137-885e-5ee9-99d4f917a7f9@gmail.com> Date: Sun, 12 Apr 2020 23:03:53 +0100 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:68.0) Gecko/20100101 Thunderbird/68.7.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit Content-Language: en-GB Subject: Re: [PHP-DEV] [DISCUSSION] Match expression From: rowan.collins@gmail.com (Rowan Tommins) On 12/04/2020 21:16, Ilija Tovilo wrote: > >> Can match arms contain other flow control, such as "return" or "continue"? > Yes and yes. They behave exactly the same as in the switch. This > should definitely be described in the RFC. You didn't quote the second part of that question, which admittedly was a bit rhetorical, but important: allowing these rules out using either of those keywords in future extensions of this syntax. That means if we want to add a way to get a value out of a statement block, we can't use "return"; and if we want to add explicit fallthrough, we can't use "continue". Also note that in switch, "continue" and "break" are actually synonyms, both jumping to the end of the switch block (see also https://wiki.php.net/rfc/continue_on_switch_deprecation). I'm not sure that's actually all that intuitive or useful in the proposed match statement. >> Can I mix a bare value and a block as arms of the same match, or does the whole construct need to be in either "expression form" or "statement form"? > Yes, you can mix blocks and expressions given you don't use the return > value of the whole match expression. Again, you skipped the second part of the question - will this make sense to users? What error message will they get if they write this: $x = match($y) { 1 => "Hello", 2 => "Goodbye", 3 => { throw new Exception; } }; (If https://wiki.php.net/rfc/throw_expression passes, then "3 => throw new Exception" without the braces will presumably be valid, but that just makes it even more confusing, IMO.) >> Can I omit the trailing semicolon even in "expression form" > Yes. So this would be valid PHP? $x = match($y) { 1 => "Hello", 2 => "Goodbye" } echo $y; > To clarify what's going on here: There's no statement variant of > the match expression [...] I don't think users will see it that way. An "expression" where you can't use the result (but can use flow control like "throw" and "return") feels very much like a statement, regardless of how the parser defines it. > ... because I noticed that I kept forgetting to add it in my tests. I think needing to add a special case to the grammar is a sign that the proposed syntax doesn't fit well with the rest of the language. Did you ever forget to put the semicolon when using it in expression context, or only when using statement blocks? > As mentioned in my announcement IMO it doesn't make sense criticizing > the switch statement but then not offering an alternative. I don't think this is true. If you were proposing "foreach", you might criticize the "for" statement, but that doesn't mean you'd extend "foreach" so that it could replace every single "for" statement. To reiterate, I think the match expression - with no statement blocks - stands up really well as a replacement for those switch statements where you're deciding on a value, and for nested ternaries (which are pretty much unused in PHP due to the broken precedence). Other proposals can replace other uses of switch, or switch can live on with more limited use - after all, we even have a "goto" statement in PHP, for those rare occasions where *none* of the built-in flow control is good enough. To respond to something you said to Larry: > People can't be forced into > writing clean code and my fear here is that they will just revert back > to using what is more convenient. That's their loss! Whether or not "match" supports blocks, some people won't use it, just as some people write unnecessarily complicated if statements because they don't spend the time structuring them with elseif or switch. My concern is that if match supports blocks, but the syntax and behaviour is unintuitive, we'll be stuck with another feature that nobody likes very much but we can't easily replace. Worse, the RFC might lose support based on that part, and we'd miss out on getting a really nice new expression. Regards, -- Rowan Tommins (né Collins) [IMSoP]