Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:109607 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 28323 invoked from network); 13 Apr 2020 16:39:47 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 13 Apr 2020 16:39:47 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 478AD180545 for ; Mon, 13 Apr 2020 08:09:13 -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.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,HTML_MESSAGE, MALFORMED_FREEMAIL,MISSING_HEADERS,RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL,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-ot1-f66.google.com (mail-ot1-f66.google.com [209.85.210.66]) (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 08:09:12 -0700 (PDT) Received: by mail-ot1-f66.google.com with SMTP id m2so9399966otr.1 for ; Mon, 13 Apr 2020 08:09:12 -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:cc; bh=kgvyU9wnD7YEYx2GpHH+vZbBlS1B9aDLr3wxVbjNG+g=; b=Oa6492DtyPN6xHHRPPLtXlo3Sn4oJg/d+KzyM+ivsIQNDS058+7xxakSLLutSZAr95 Whu424SGBT8hjlTrb08yKsrVWek8UBXno7ccYeU54Nt2ZZNuE8Nax+V9r6hOH4MNvmz4 relIF3kPUWVg19yKqkQ3cf63LA9xAp0d0IASkUJttCNMO69zbgJjwGU7KM4p4AncLuvY MakR662GeRRNd+QiUNfLDwgs0ExcakxBwS0dXkGO9d90pZ7QQYLsb90bldyTPZaPXghW Y2gCRp+LGHpFOZul77h1yKi0cnP5yFQWbQTXUOzuI3fth9vyY2src/xPlzec9xg9wLTf uP+Q== 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:cc; bh=kgvyU9wnD7YEYx2GpHH+vZbBlS1B9aDLr3wxVbjNG+g=; b=rgtEAV9xVRHH8pXUfNS9/UvlJ84cteKxggJPcmbZJqy1v62sOOweiLsbggOnNZg2LT dmR/6VTT4TQ+KM3ntDghhOi34eZqWj0wy3s3kKSe4edLqhspnf6jPSVVx6K3JAll9j77 BMn68yQEx2Gzue4XWddHBzy1a1BAOlhdV7LGtR5maHDRh/iDg6ebPzmCAz3xijvkHTcI FOdWSkjkpLFdmPJi2VnmmwcXUKYETNRx32Qbt4UTq3PxiKq6DdLY2VNLc5ID/cdQ22WG 8+uB0aOJN1L7/LvqxnULqwq3U25ux3K5Vw1bf66giylnwiVswwE/OOEojY2L9ai/x9C6 v6Xg== X-Gm-Message-State: AGi0Pub/AsfqjlZYRRacklClf8GViyku9lcDrzf/oJfgghFODeoh4jEL uykl8k+78xmA67ricBOYvuIW1ZAxIQCAWn7QJQIODdueoxs= X-Google-Smtp-Source: APiQypJx/tc0ndLQmSxrGpVXD3rI6OyVbUK+tABdH2SCx7G7T8Owvjlq9l4wq5pDn95CpPnbPkcLiX/SFU7wkSFm8e0= X-Received: by 2002:a9d:453:: with SMTP id 77mr15574547otc.230.1586790549268; Mon, 13 Apr 2020 08:09:09 -0700 (PDT) MIME-Version: 1.0 References: <6aebe21d-2137-885e-5ee9-99d4f917a7f9@gmail.com> <5071DCCE-4572-4B1F-A50B-832C5EA74795@gmail.com> In-Reply-To: Date: Mon, 13 Apr 2020 12:08:57 -0300 Message-ID: Cc: PHP internals Content-Type: multipart/alternative; boundary="00000000000005b02205a32d7746" Subject: Re: [PHP-DEV] [DISCUSSION] Match expression From: david.proweb@gmail.com (David Rodrigues) --00000000000005b02205a32d7746 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable With all the humility of the world and without wanting to be exhaustive about this, my only question is why can't we keep it as a switch, instead of creating a new keyword? $x =3D switch ($y) { case 0: return 1; case 1: return 20; // default: return null; }; I say this because, in the future, we could do something similar for if(): $x =3D if ($y instanceof Foo) { return $y->bar(); } else if ($y instanceof Bar) { return $y->foo(); }; // else { return null; } Or even for loopings: $x =3D foreach ($items as $item) { yield $item; }; // $x =3D=3D $items So we would not need to create a new keyword for each equivalent type when used as an expression. The only reason would be something like "switch has no return support", but this could happen exclusively in this scope, just as "use ()" exists only in functions and not in methods, which are basically the same thing, however, in different scopes. Likewise, "break 2" would be impossible in this case, so just issue an error saying "break n is not supported in this situation". Atenciosamente, David Rodrigues Em seg., 13 de abr. de 2020 =C3=A0s 11:49, Rowan Tommins escreveu: > 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 =3D> ['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 =3D> 'print', 2 =3D> 'var_dump' } > ($x); > > // Invalid under the proposed rule, but a valid method call if match was > always a normal expression > match($x) { 1 =3D> new Foo, 2 =3D> new Bar } > ->doSomething(); > > > To be fair, the same limitation exists for closures, where this is invali= d: > > 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 =3D> 'print', 2 =3D> 'var_dump' })($x); > > (match($x) { 1 =3D> new Foo, 2 =3D> 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=C3=A9 Collins) > [IMSoP] > > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: http://www.php.net/unsub.php > > --00000000000005b02205a32d7746--