Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:109299 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 33875 invoked from network); 25 Mar 2020 17:57:04 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 25 Mar 2020 17:57:04 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 31A571804E0 for ; Wed, 25 Mar 2020 09:21:46 -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,HTML_MESSAGE, 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-il1-f174.google.com (mail-il1-f174.google.com [209.85.166.174]) (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, 25 Mar 2020 09:21:45 -0700 (PDT) Received: by mail-il1-f174.google.com with SMTP id k29so2432362ilg.0 for ; Wed, 25 Mar 2020 09:21:45 -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:to; bh=WEvdk+ts0sE6R9bFjZSgvc4Np8W++7NGbvHmI2F00QA=; b=q+4SI8Hvl9SW4eMJSmsv8CkH3yTUy2vBaNvyGJ9nL9SSxGr0Ev7lbVzW4QfqP6Uq1n fVRHYdc0aSA86NE8859MWTo2y9JmbrKB0i4DJc8p0BJym/ha+Ph8UIgdgjN/rKnGEXQR zzQOViGVJxJuB9Zql57ySH/JT+lJMu7GH6OF2DDdORPFjXquR2W/KalONwCmizp1QmD6 pFEVeBY4Hu9Yc5VftkKu2cFu7rL4LbrxXv4WpYLMYlKCVcF4mvA47vHMg9ascZ2Ew3wU nwz0nzp8iXQQGteKsGAFbIGaRyz7xHlvEjwtv9eOP29hVLKjmS23PfAkRcICSLZ2v1l3 Hmkw== 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; bh=WEvdk+ts0sE6R9bFjZSgvc4Np8W++7NGbvHmI2F00QA=; b=hoy6J+ns1oD/apfw9hAmE1/dv58DYSRl5vXSCtfOnSfiDsTpgBLHqIWrHHe9/R1nh/ wXfznv7PmmrPEMKemoarFqXoWtRo0po/BaXKiquxqS+W0yEseFOpGNgYJeEHE02XggIW /wfYxLcBn7Fqu5eciKhkytLKHq6TdvQ8Fcf/M0wYSU+0F76n/uuHlAL86Vv390ws+qt6 HNqE0bEnZTmAEseUlGmoOIsBolj9bLJ/kH+2Z7CryB39mmAk8+oVsc/EImL8QjQGPT9Z HxcAhBhMvpuMTR2EdBnHSSRaZYdOmF0v4WG6jMIiUGopplH380X4Fdca2FzoGFlv6Ow3 WH3Q== X-Gm-Message-State: ANhLgQ0MeO9QvrXgQb0ver4T80qxkPb0JzkPWdkXGf3F/gGrJk9FvKui Q5qjvxgoIyK6xIQafRuUgQFnUbGb98LNFb/vCOaTB88n X-Google-Smtp-Source: ADFU+vsAQQimHl8FmAYLmAHVyMKp+7S4XMWkLDUXvsY0damMlbEo3Ut0FU3gub0yD14Kf72v4cUbv9zKle0Tc26ZDmc= X-Received: by 2002:a92:db04:: with SMTP id b4mr2530901iln.120.1585153301610; Wed, 25 Mar 2020 09:21:41 -0700 (PDT) MIME-Version: 1.0 References: <047092C7-84FB-42AB-8084-7B83F76F55C1@me.com> <4513B88E-CA5B-4DF2-94C8-242BEE54ADCC@me.com> <767910ef-d27b-404c-9ad1-037105c69d12@www.fastmail.com> <2E98378A-605D-40ED-898C-6ABBFBD091B9@me.com> In-Reply-To: <2E98378A-605D-40ED-898C-6ABBFBD091B9@me.com> Date: Wed, 25 Mar 2020 16:21:29 +0000 Message-ID: To: php internals Content-Type: multipart/alternative; boundary="00000000000075046f05a1b043be" Subject: Re: [PHP-DEV] [RFC] switch expression From: rowan.collins@gmail.com (Rowan Tommins) --00000000000075046f05a1b043be Content-Type: text/plain; charset="UTF-8" On Wed, 25 Mar 2020 at 15:29, Ilija Tovilo wrote: > I don't think this would add any significant benefit over: > > ```php > $x = true switch { > $x !== null && $x < 5 => ... > } > ``` > The problem with that is that it requires a temporary variable to be switched on. If I want to switch on, say, a method call, I can write this for equality: $result = $this->foo($bar) switch { 1 => 'hello', 2 => 'hi', 3 => 'goodbye' }; For inequalities, the switch(true) version looks something like this (the parentheses would probably be optional, but I'd personally use them for readability): $temp = $this->foo($bar); $result = true switch { ($temp <= 1) => 'hello', ($temp == 2) => 'hi', default => 'goodbye' } Using $$ to mean "value tested" would mean you could get rid of the temp variable, and just write this: $result = $this->foo($bar) switch { ($$ <= 1) => 'hello', ($$ == 2) => 'hi', default => 'goodbye' }; I've also previously thought about specifying an operator for switch statements, which could be used with expressions as well, e.g. $result = $this->foo($bar) switch <= { 1 => 'hello', 2 => 'hi', default => 'goodbye' }; Again, though, this is something that could be added to switch statements and expressions together, as a separate RFC. Regards, -- Rowan Tommins [IMSoP] --00000000000075046f05a1b043be--