Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:109394 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 97355 invoked from network); 28 Mar 2020 17:40:39 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 28 Mar 2020 17:40:39 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id C118B1804E6 for ; Sat, 28 Mar 2020 09:06:05 -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-yb1-f180.google.com (mail-yb1-f180.google.com [209.85.219.180]) (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 ; Sat, 28 Mar 2020 09:06:05 -0700 (PDT) Received: by mail-yb1-f180.google.com with SMTP id d5so6366073ybs.8 for ; Sat, 28 Mar 2020 09:06:05 -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=GsWkgMQO9BnAuMN4LfWhCgfrOHZow51iqNfgZ319QVM=; b=NmZ6rNkHEo4Qq4OHnOCoV//AVuEYYatap/9aUhsJodrwANIGcYYvA7O9n4KEMCpHUW HxEnBCq3BUkMCwvHV1VEzt4InRvIGvcMWhMPMtDRhlcv1P28sXsZzX+7j4j9Ujty/kVn zeUYCTHR9b41Yo439DA8uZ0jyMO+tiZgBla+GQh3lZEJ44E69vaIBih52hdCIw0gk/Qy bFWqfoZu7iZk8VJXonaE/cK/XhpB4/eGk3QcbAQEfFY3HnFPphPDL0H6KBZJozhUItPe iyCsFvz2ryadWRLc7pFgG2QhAcLuXeCwP5k200tXg7qkvVPtHpd1IqrS9nI0r/0cMrDH 7y1g== 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=GsWkgMQO9BnAuMN4LfWhCgfrOHZow51iqNfgZ319QVM=; b=Fjr6XvYKpnHWgi2l8yt62aO+QMl7En4vVnzgrRpOGjmS31OiYGdkVW3Wd+jGQf4qqX zXG6N9IY2QKD4/c9OO3ozNipT74xuadJK2F3Z4AjriZUAkeaHO9P9KNMDrduXfah7Ycg 1rmWYH9Lx+JtU3nw+2WAL5HDxsqMH2Iq8pcUQYAmjuikIKpUUesTTSnkKNB9AJDGOMrv vnmSNez3K5F6SYYw2K42UY+vDkWibe6kkQfkc2AKiUx00KFlTisF/Dj3ckAYBe5Jc6xG teoStG6rmublrCjc4Bd2bf8QZLgFFDqR5sFtLwTSfUdwW5LSSL5IxEdjTRBjY2GcUiVM PHuA== X-Gm-Message-State: ANhLgQ1PUUDkanRoBHNFZA4KBh8dkUvffAvAaFhzygD9IXN7a+M/yJJ8 TVzC1xPVI+wPbOSthCzeuu9T6xboIJWnCvKrMqH/lMPP X-Google-Smtp-Source: ADFU+vsQHZPjzlNB9wuwYNEARtkNIFLMJyjjU9Uj9uRYa9K6G2nu5y5vcMSEEzeHuJmuy/D7JS0om/hMCTh77/iqyRA= X-Received: by 2002:a25:3bd0:: with SMTP id i199mr6480361yba.297.1585411563903; Sat, 28 Mar 2020 09:06:03 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: Date: Sat, 28 Mar 2020 17:05:52 +0100 Message-ID: To: Dan Ackroyd , PHP internals Content-Type: text/plain; charset="UTF-8" Subject: Re: [PHP-DEV] [RFC][DISCUSSION] switch expression From: tovilo.ilija@gmail.com (Ilija Tovilo) Hi Dan First of all, thank you for your feedback. I appreciate your time. I think it's worth noting that the switch expression solves the problems it does implicitly, not directly. 1. Returning values: Of course, this is the main argument for the switch expression (regardless of the other issues present in the statement version). 2. Fallthrough: A fallthrough for the expression version doesn't make sense because the point of every case is to return some value. A fallthrough would cause the value to be discarded, returning the value from the next case that breaks. Furthermore since every case only accepts a single value there's not even a way to express a break right now (as it is a statement). 3. Inexhaustiveness: The switch expression has to evaluate to something. When no case is executed we can either make it return null or throw an exception. Since it is better to be explicit I chose the latter. The only issue that doesn't have any direct justification for being different from the statement is the type coercion which is why I decided against changing it. It's a one line fix so believe me that I was tempted. But I think it's important to keep them consistent. > 1) The behaviour of switch with regards to type conversion is the > biggest thing I care about. If the RFC isn't going to touch that, then > it's way less interesting to me. I understand. As you mentioned I'm trying not to make the distinction between the statement and expression arbitrary. > 2) In general, I think that using the switch keyword with slightly > different behaviour is not a good idea. Even if we had 'editions' in > PHP, having subtly different behaviour is very confusing. A better > approach would be to use a new keyword (maybe 'select') that has the > correct behaviour from the start. I used the switch keyword for the simple reason that it very much still is a switch. It uses the same AST, same code generation, same opcodes. > 3) Fallthrough: > > "The fallthrough behavior can't reasonably be changed in the switch > statement because it would break a lot of code. However this RFC > porposes allowing multiple conditions per case so that the intention > of running the same code can be expressed more clearly. " > > I don't think this is the right approach. Most new languages have the rules of: > > * each 'case' statement has an implicit break statement at the end of that case. > * they provide an explicit 'fallthrough' keyword for where fall > through is desired. This sound like an extension of the approach described in the RFC. There's nothing stopping us from allowing a fallthrough keyword, although I personally don't see a huge need for it. Either way, the fallthrough keyword still wouldn't make sense in the switch expression. > * btw it might be worth thinking about pattern matching as well. I've mentioned pattern matching briefly in my original email. There are a few use cases in PHP right now, namely array destructuring, class destructuring (public properties) and ranges. Most PHP libraries make extensive use of accessors so at the moment I don't think the complexity of the feature justifies the limited use cases. This may very well change in the future (for example if PHP ever gets C# style get/set accessors). > Currently the below is supported, but is also horrific coding (imo) After this RFC your function could be rewritten like this: ``` function checkValue(int $x, bool $errorIfOverTen) { return true switch { $x < 5 => "too small\n", $x > 10 && $errorIfOverTen => "value is over ten, which is not allowed.\n", default => "Ok\n", }; } ``` which doesn't look too bad IMO. It has been suggested to make the true optional in this case. I still haven't fully formed an opinion on that. It would make the syntax ambiguous again for sure. I hope that clears up some of the points you've mentioned. Ilija