Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:109606 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 25084 invoked from network); 13 Apr 2020 16:20:13 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 13 Apr 2020 16:20:13 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id CAFA31804F3 for ; Mon, 13 Apr 2020 07:49:38 -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=-2.1 required=5.0 tests=BAYES_00,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-wr1-f41.google.com (mail-wr1-f41.google.com [209.85.221.41]) (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 ; Mon, 13 Apr 2020 07:49:37 -0700 (PDT) Received: by mail-wr1-f41.google.com with SMTP id a25so10434531wrd.0 for ; Mon, 13 Apr 2020 07:49:37 -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=I/ypwaDvtHP4eJMnP83MVzLJYNiAyZZ2TBggIma9je0=; b=SxhD5gAKRbSUMUyvxdyR1IyqGsb6RPPVo0DAmnctjU8isGl1W4hpWuet/UKLpsH0B7 C8K++rgkY+eezppPTiZ36sSyEnQRnyvXzsbMUOWY63BhI/AFRPulIbOkLGPK9EKghoqk tGPvXNwcfWar9nLikAW4d7v9tnvQCqAKGpzmZHCSAEXywTxkLK3oPrtS/rmun5pfDG5S w9Q0ugBfRx7geh5cCahgMj/56mK44VKfK1trRqf0i3BGlYkqK15UxEXiG9ocxXA7hz/b xNVp4bFA/u4yhfzmDBnTAMRBShEnFe6An2sVnQGWAveBldGtLHgi/TqRG2NTlE8q1Wo0 qBYQ== 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=I/ypwaDvtHP4eJMnP83MVzLJYNiAyZZ2TBggIma9je0=; b=mVsVaLYARqEVyg//HhL2WVj3jRG5D3MLVm1Itu2SfiAOOfpuYQaRFqkbcoO9PgvpI/ UaxKA+vQWcNZxNFIlxf46obHS6/hI89I/M+lYyRWAy5EtahSaKWsLYVLUsJSaxZ3ORpg kCs4rSn0Hwspb5hLO0P0xrsLXZU2ZbkxRqH6FZIHbtGgrSGTcWmUjnDOwYgnP2L7NGtq PxAV/5lKOtUdSDEsdYB60unH9Kl9ieZ70M5KKB/Fsnk74leHOYBLKCZlFWYno3wbMMOh s6P+GwIXckJ8VhUlsNRdr6gPHE3IZe962o4khdKql/KpsonS76DpEhBlty0SHUiLP+WG qQ2A== X-Gm-Message-State: AGi0PuZwbmqDK4vKjohFMajZKFpTtYp8/533Q3Q9a1pfcpZO2BNKeKf/ iUjrWwNH1huSboDyh+4QdzDNWxXv X-Google-Smtp-Source: APiQypJ5iPO+oWJAxP3meH3z2eUym5ZZaful71wVRwG9M2t+5ZPEORUrXvvVT4e4k4/T7CQRQnQsMg== X-Received: by 2002:a5d:54ce:: with SMTP id x14mr2933835wrv.103.1586789376497; Mon, 13 Apr 2020 07:49:36 -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 v1sm10321848wrv.19.2020.04.13.07.49.35 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 13 Apr 2020 07:49:35 -0700 (PDT) To: PHP internals References: <6aebe21d-2137-885e-5ee9-99d4f917a7f9@gmail.com> <5071DCCE-4572-4B1F-A50B-832C5EA74795@gmail.com> Message-ID: Date: Mon, 13 Apr 2020 15:49:36 +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 13/04/2020 12:28, Ilija Tovilo wrote: >> It would be possible for the blocks to return values (though not with >> the return keyword). >> [...] > We can't do everything at once. Then why not introduce the match expression first, and add block expressions (as a general concept) later? That way, we don't need all the special restrictions on when to use them, and can work out a consistent set of rules for them which can apply language-wide, rather than being constrained later by decisions we make half-implementing them now. The ability to use them with short closure syntax would be very interesting, if the scoping/capture rules could be agreed. >> it makes *all* the control flow blocks in Rust feel more natural to >> developers coming from other languages. > Same thing here. If you don't use the result value, it looks just like > the switch. That's why I'd feel odd to require a semicolon. Not really. In Rust, it's about making a whole bunch of control structures, which are already consistent with each other, more user friendly (but still consistent). In your proposal, it's about making one specific control structure look slightly more like other syntaxes in the same language, by adding a special case which applies nowhere else. To be specific, in PHP, there is no "omitted semicolon" at the end of "if ($x) { y(); }" - that is just how the syntax is defined. There's no need to have a parser rule to allow it to look like C or Java, because it already does. > This code is parsed like this: > > ``` > match($x) { 1 => ['foo', 'bar'] } > [0]; > ``` > > * statement list > * match expr > * array literal > > Note that we can't have ambiguous grammar since Bison would > immediately expose it. I'm not sure what you mean by "we can't have ambiguous grammar" - the code above *is* ambiguous, in that it could be defined to mean one of two things; what you've shown is which definition your implementation defines. That resolution makes sense for that example, but consider some others: // As proposed, parsed as two meaningless expression-statements; as a normal expression, would select and then invoke a callable match($x) { 1 => 'print', 2 => 'var_dump' } ($x); // Invalid under the proposed rule, but a valid method call if match was always a normal expression match($x) { 1 => new Foo, 2 => new Bar } ->doSomething(); To be fair, the same limitation exists for closures, where this is invalid: function($x) { echo $x; } ("Hello"); and you have to write this instead: (function($x) { echo $x; }) ("Hello"); So I guess the above would follow the same rule to force expression context: (match($x) { 1 => 'print', 2 => 'var_dump' })($x); (match($x) { 1 => new Foo, 2 => new Bar })->doSomething(); The only downside is that omitting the parentheses wouldn't be an error, as with the closure case, it would just silently do nothing, which might be rather confusing. Dropping the special semicolon handling would mean it either did the expected thing or gave a parse error straight away. Regards, -- Rowan Tommins (né Collins) [IMSoP]