Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:112532 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 22416 invoked from network); 17 Dec 2020 01:18:42 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 17 Dec 2020 01:18:42 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 7E39F180384 for ; Wed, 16 Dec 2020 16:50:00 -0800 (PST) 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-Virus: No X-Envelope-From: Received: from mail-wm1-f54.google.com (mail-wm1-f54.google.com [209.85.128.54]) (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 ; Wed, 16 Dec 2020 16:50:00 -0800 (PST) Received: by mail-wm1-f54.google.com with SMTP id n16so2925131wmc.0 for ; Wed, 16 Dec 2020 16:50:00 -0800 (PST) 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:content-transfer-encoding; bh=9MYrQu2WL4DBBn9sBfzJs+8av4F26K1++BhmyafO+vE=; b=ngV40ghTf70c7zyGX2TzGLGioHQyQ7z1MHfw1TEkPGf7CAnEPme4bGcOx1rntaIY6D 13n6/7I0iaCcI+3iZAi5PK0ObNB0hrO3E7mkyRA1We2IDII1LwiNQegDjCyPczqmY34w j5FJ1TjyeUvgWsAirvpk+ySSjiTnW9iBSYCyBMqn32hUbkyhP9NrO7uy1LeYBp63pHDd 5WxPGBEm4SG3qCTrwwtiFhFtGGPLE+z9M1I1wtGejdi1CcO9SakOBGI4N54QnhR71qut wTcKWfNWFyHmAHUzqHW927wPaZjiOoFXkKxWL1Ow/rFuKyUZhE8G+V+nu91jNfCXvcFa inJw== 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:content-transfer-encoding; bh=9MYrQu2WL4DBBn9sBfzJs+8av4F26K1++BhmyafO+vE=; b=c3ozjPmSKEGOIp9ax9k/WjP3c0w5ggwbTFTWQjEmitP9Af6E607TNdXYkvsvdKsWNl NQVAvFYa2BOVFXcUcuyTkdIXK7/ZaJY2bQqN4+x23eiUp0H2Vpx+2KzSjZpLhvvMvMep FqRQNoKJNDH+Y0W7P6SzEA4IeI5viNJYjBFFZbKt0JifcJz8HdDX2nxGo59mhVKiFgrX 5DQtcKkIkbKSCWy8HGpbw4wHmH5OxF9vCSx9jn0WsQ/Ay7yeB/P7NRfr7KtBW/Fb1HAV PRNEpT14Q1ds1GFTnYLSpRBZFTjvtwiOZkp2XqeqLtqocia4QGE53x9bqyySyWpNsdvX Ip+w== X-Gm-Message-State: AOAM533//4aRM1H0fQ7CdxHvET6NYo8sUX1CtNWzAixiQd/FRAFa5xsM /NrP2tx4pThIvStwbpUbFAGXSt4EY1cL2OJ5YoI= X-Google-Smtp-Source: ABdhPJx2mowSXiabubzoin3EjBJ+/AFf0zLz01nmDvRsTSSPHgQSPHOdUEP6SPMa6JapkP7yK07Cj+aGD6GSZeCO5Ps= X-Received: by 2002:a1c:e084:: with SMTP id x126mr5783089wmg.109.1608166198026; Wed, 16 Dec 2020 16:49:58 -0800 (PST) MIME-Version: 1.0 References: In-Reply-To: Date: Thu, 17 Dec 2020 02:49:46 +0200 Message-ID: To: Marco Pivetta , Larry Garfield Cc: php internals Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Subject: RE: [PHP-DEV] [RFC] Short-match From: someniatko@gmail.com (someniatko) `match` is an expression, where as if-else construction is not. This allows for combining it with a potential future feature of single line functions and methods. For example (hypothetical syntax is used): ``` function getNumberKind(int $number) =3D> match { $number > 0 =3D> NumberKind::POSITIVE, $number =3D=3D 0 =3D> NumberKind::ZERO, $number < 0 =3D> NumberKind::NEGATIVE, } ``` See how natural it looks and reads. Another improvement is that it reduces the amount of visual clutter (`if` and `return` keywords). Yet another distinction between =E2=80=9Cif=E2=80=9D and =E2=80=9Cmatch=E2= =80=9D is their intent. `If` is more appropriate for the procedural flow. However, when we operate with pure functions, `match` is a good sign of such function. It shows that different input values turn into different output values, instead of different branches of procedural code being executed. Therefore, if code is intended to be more FP-ish, IMO, `match` is preferred. For the procedural code patterns, including early returns / throws, `if` is clearly a better choice. So, `match` is better suited for functional-ish parts of code, which do not pose behavior, but only convert one value to another ones. In the FP world, pattern matching is a widely used feature, allowing to split computations of output value for different kinds of input value. The closest thing available in PHP these days is `match (true)`. The `(true)` part looks ugly and artificial, and removing it is a matter of a cosmetic change. Of course, it is possible to use `match` instead of `switch` or `if`s for procedural code, and vice versa, which is still syntaxically correct, but bad from an intent expression perspective. This is a downside of `match` expression as a whole and, IMO, is irrelevant for this small improvement. So, - `match` allows writing clearer code when used with FP-ish intent; it is also an expression, therefore irreplaceable with if/switch in some cases. - pattern matching is a widely used feature in FP, and `match (true)` is the current way to achieve the most similar thing in PHP. - improving `match` will not hurt the language allowing to write the same thing two ways =E2=80=93 because it is already done by accepting `matc= h` RFC itself