Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:102278 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 74584 invoked from network); 15 Jun 2018 15:31:38 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 15 Jun 2018 15:31:38 -0000 Authentication-Results: pb1.pair.com smtp.mail=larry@garfieldtech.com; spf=permerror; sender-id=unknown Authentication-Results: pb1.pair.com header.from=larry@garfieldtech.com; sender-id=unknown Received-SPF: error (pb1.pair.com: domain garfieldtech.com from 66.111.4.26 cause and error) X-PHP-List-Original-Sender: larry@garfieldtech.com X-Host-Fingerprint: 66.111.4.26 out2-smtp.messagingengine.com Received: from [66.111.4.26] ([66.111.4.26:35215] helo=out2-smtp.messagingengine.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id C3/D4-29356-8DBD32B5 for ; Fri, 15 Jun 2018 11:31:37 -0400 Received: from compute7.internal (compute7.nyi.internal [10.202.2.47]) by mailout.nyi.internal (Postfix) with ESMTP id B7CC821BAD for ; Fri, 15 Jun 2018 11:31:34 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute7.internal (MEProxy); Fri, 15 Jun 2018 11:31:34 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to:x-me-sender :x-me-sender:x-sasl-enc; s=fm3; bh=+2kAchtlJ5EsOWRtHIOsq1HpblBCm m54WGDC/IfYUHY=; b=RUQRY5utBhQiC2h9iBizX1myOhwCLunbTTfKtZpsOKvkF X5Toievs6HRHaQ5K+dkRj7BxwCakeATMB5zasvzREMvdfBNhssqAN+iOSUikkQly VcQzEDXapncjRoGGBS/hiZtm1VA4HfmLrPtsl2EGIGMvpLscGuI3GGgKA7Gsndfu DN/LhMSQfFT/uKgy2QuQ0oK5g7JtEIcdlWQ6GIdyntNAinQO2CnOGl66c/dL8TZO nhX81niww04cjghqPAek46FehkenYo6tbtM5Q5Vo7E6OMLCk97OUIwk5Vn/H6C1x 3wk+96cz0zMfn2doGm4KvJiznJF0PDeXl/30+baBA== X-ME-Proxy: X-ME-Sender: Received: from vulcan.localnet (216-80-30-152.s3222.c3-0.frg-cbr1.chi-frg.il.cable.rcncustomer.com [216.80.30.152]) by mail.messagingengine.com (Postfix) with ESMTPA id 32C66E425A for ; Fri, 15 Jun 2018 11:31:34 -0400 (EDT) To: internals@lists.php.net Date: Fri, 15 Jun 2018 10:31:31 -0500 Message-ID: <2525476.IYyWTAfRZ0@vulcan> In-Reply-To: References: MIME-Version: 1.0 Content-Type: multipart/signed; boundary="nextPart4015572.ypmF5eeSWI"; micalg="pgp-sha512"; protocol="application/pgp-signature" Subject: Re: [PHP-DEV] Strict switch statements From: larry@garfieldtech.com (Larry Garfield) --nextPart4015572.ypmF5eeSWI Content-Transfer-Encoding: 7Bit Content-Type: text/plain; charset="us-ascii" On Friday, June 15, 2018 9:54:48 AM CDT Theodore Brown wrote: > On Thu, Jun 14, 2018 at 4:35 AM, Nikita Popov wrote: > > I think if people want to use strict matching, they'll quite likely want > > to > > have it on all cases. Something like "strict switch ($expr) {}" or "switch > > strict ($expr) {}" or "switch (strict $expr) {}" or "switch ($expr) strict > > {}" or "switch ($expr) { strict; }" or whatever would be preferable in > > that > > case. > > > > Additionally, switch has the issue of fall-through behavior, which is > > somewhat unexpected and error prone to many people. It might make sense to > > introduce an entirely new "match" statement that conforms a bit more with > > how switch-like strictures are implemented nowadays. That is, something > > like > > > > match ($expr) { > > > > "foo" => {...}, > > "bar" | "baz" => {...}, > > > > } > > > > or similar. This might need some more design work to ensure forward > > compatibility with potential future algebraic datatypes etc. > > I really like this idea. It's similar to the `match` control flow operator > in Rust: https://doc.rust-lang.org/book/second-edition/ch06-02-match.html. > > One reason I almost never use the `switch` statement in PHP (besides the > lack of strict matching) is that it's so verbose. Having to put a `break` > keyword after every case to avoid fallthrough means that it almost always > requires less code to use `if` statements instead. > > To avoid confusion with union types, I think I would prefer commas between > or conditions instead of a pipe character. Example: > > ``` > match ($expr) { > "foo", "bar" => {echo 'expr is "foo" or "bar"';}, > true => {echo 'expr is true';}, > 1 => {echo 'expr is 1';}, > null => {echo 'expr is null';}, > false => {echo 'expr is false';}, > default => {echo 'expr is something else';}, > } > ``` I'm not fully sold that anything is needed here, but if something is I would also very much favor a substantially more robust match statement a la Rust, Go, et al over some form of "pickiness flag" on the existing switch. Syntax details TBD, but the benefit of a flag on switch seems minimal while the benefit of more robust type/pattern matching is potentially large. That said, however, also bear in mind that "PHP is not $language": One of the big uses of match in Rust, as I understand it, is to force fully exploring the keyspace of an enum. We of course don't have enums so that use case goes away. Given the emphasis on polymorphism in OOP PHP over RTTI, are there enough non-enum, non-RTTI use cases for a robust match statement to justify its inclusion? If so, then I'm down. :-) --Larry Garfield --nextPart4015572.ypmF5eeSWI Content-Type: application/pgp-signature; name="signature.asc" Content-Description: This is a digitally signed message part. Content-Transfer-Encoding: 7Bit -----BEGIN PGP SIGNATURE----- iQEzBAABCgAdFiEE/ph/GXfY8v0YwFBp4MMKwDFxYXcFAlsj29MACgkQ4MMKwDFx YXet2ggAkDNL7asRfZ9eEc+VyllayPv5UtQ6p2OKt+89+NJyC620gItuqzrWzXAf wYbRAnEU7uLznN32g4SZnStv97a6+1YvWZ8FcHk3+T9YQ7wdaz7T/DL/BndgHUlX N9rzTfZonO5p1J4ctM1YKXRN0CKLYyC/ENznquC7wBNVPTPYaRg4je2rXaAXqNVn LHeLsqZ4/hTAhuV4Eqk9aFcxvRA7nBjcNgQf6/an3jEwYTnXIcpZaBPRuhn0X+QK umq+kt/H49Hk3UhC/xtMg6GytWwQNxl1fDI46hw2d7WeFzQc+wyDLCXPEOohRRkQ ksG1cuIn4vRFJYbk7KluAmZ1taE7gQ== =LUlO -----END PGP SIGNATURE----- --nextPart4015572.ypmF5eeSWI--