Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:107597 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 79628 invoked from network); 21 Oct 2019 11:07:45 -0000 Received: from unknown (HELO php-smtp3.php.net) (208.43.231.12) by 76.75.200.58 with SMTP; 21 Oct 2019 11:07:45 -0000 Received: from php-smtp3.php.net (localhost [127.0.0.1]) by php-smtp3.php.net (Postfix) with ESMTP id 1B4022CBF5F for ; Mon, 21 Oct 2019 01:51:23 -0700 (PDT) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on php-smtp3.php.net X-Spam-Level: X-Spam-Status: No, score=-2.0 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FROM,HTML_MESSAGE,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE autolearn=no autolearn_force=no version=3.4.2 X-Spam-ASN: AS3215 2.6.0.0/16 X-Spam-Virus: No Received: from mail-io1-xd2b.google.com (mail-io1-xd2b.google.com [IPv6:2607:f8b0:4864:20::d2b]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by php-smtp3.php.net (Postfix) with ESMTPS for ; Mon, 21 Oct 2019 01:51:22 -0700 (PDT) Received: by mail-io1-xd2b.google.com with SMTP id i26so5605216iog.9 for ; Mon, 21 Oct 2019 01:51:22 -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=VnjhGQpYifB2GAbNVrrRch1owYuqe6PA9B2btL2gLo8=; b=uSVbAqSqa034w5Kq49H0kbw5sGoU3fauh+xQB/iWjvtjda9btVMOcZEbW1DjuKZQY2 zQEuKX2JEn0oGjzCH0MXh5jgtYKt3q6gDId6yWIzZdbkWfPFj0EO2qGHoAezIE+p/a+N D2UE7PNrYZUblAKEjJLZjerdkhHAH0Nx9xtJC2bG2xIeT56LIpLn36K6u2gl8hA6hcCo a85pXGq3+5Rj8zcLPq5uBZc6iOFkLhCB129tjvuV5NaFGZyWR5NyRYLCNBYo1rDCt88p SuvStV7AIErVBiKU2c6Ux6nMx2USAB3PDVPEHLtjIL+fawCGjLvu606LdTBnLyrg9b0E DOXA== 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=VnjhGQpYifB2GAbNVrrRch1owYuqe6PA9B2btL2gLo8=; b=OVLVA8VHycZc9Vu3BBzcYiSwc3pKitJc3h5/kppmsLmVirf+DCTZrc95StEjdEhtE1 +RpaYsAlNgOM9n07L2kRGVK4vY+Gfg+uSJ2W33xOKx4at/uKT8i7UaWCVMjAoR1L/7IO 8tElwLQyHqV/7VD6YVYdv13LVIkNZ+j3ZuWPlAeth9RyFWXeoicDOo9l56Ox6BLOSuWn zpEibmqbjQCfBqddSZpIdVwHFD5a8KylXffCuiLoVlGJWD1uVI/lakXx3I23pciXiE6F l/eOHr6hYNbpUSxayERNRpDF2XOsUHgnLnWErJiOADFQrQ/+bRriArfKCXT7NZ7kELIV lzLQ== X-Gm-Message-State: APjAAAXntxSIvvCFh+YdtwZjiD74kMX8Ei4kmZ/9t1vSP8aOA06eXhXN 8O/5iXJiaT4sh2rgjOvh/kJFqkOzKuxCN2b/W28oSCb9 X-Google-Smtp-Source: APXvYqw4jMgjraUltn8Re9BfEmIXbR+I+zwCDTvepifarsTG8AD24ESWh4NKtQsd6X72CZQGtfrwl37aVUlqCMpUguQ= X-Received: by 2002:a02:a792:: with SMTP id e18mr17007855jaj.88.1571647881582; Mon, 21 Oct 2019 01:51:21 -0700 (PDT) MIME-Version: 1.0 References: <91095002-BA0D-440B-8FAB-AA003A0EDB69@newclarity.net> In-Reply-To: <91095002-BA0D-440B-8FAB-AA003A0EDB69@newclarity.net> Date: Mon, 21 Oct 2019 09:51:10 +0100 Message-ID: To: PHP internals Content-Type: multipart/alternative; boundary="000000000000b19dc4059567c948" X-Envelope-From: Subject: Re: [PHP-DEV] 'switch-expression' and the 'type guard' unary operator demo From: rowan.collins@gmail.com (Rowan Tommins) --000000000000b19dc4059567c948 Content-Type: text/plain; charset="UTF-8" On Sun, 20 Oct 2019 at 22:35, Mike Schinkel wrote: > What restriction are you referring to? The forced `default` or the > semi-colon being required? > > Neither; I was referring to not being able to put a switch expression as a statement on its own, which Kosit has explained is a limitation of the parser. > I would much prefer to use a switch for multiple, mutually exclusive cases > no matter what how complex the expression is because with a switch the > cases expressions line up vertically. With `if` and `else if` it is harder > to vertically eyeball the different mutually exclusive cases. > I've never particularly been bothered by it, but wouldn't adding four spaces in your "if" statement make it line up with all your "elseif" statements? > The "return" case in particular seems ambiguous - if $v is visible outside > the function, will it have a value assigned to it by the "return" branch, > and if so what would that value be? > > > It should have its prior value, since the expression did not complete. > > Alternately we could choose to always assign it null on return or break. > > But how often will we have this use-case? Only for global variables, > which hopefully will eventually become extinct in userland code. > > Not just global variables, no; it could be assigning an object property ($this->foo = switch($x){ case 1 => return 1; }) for instance. It could also be in the argument for a function call ( doSomething(switch($x){ case 1 => return 1; }) ) in which case presumably the function would not be called at all? Overall, having a "return" in the middle of an expression would be very confusing, I think. > That said, we should almost be able to do *both*, and just let PHP throw > an error if there is a problem. This would be useful if you know 100% that > the code won't fail because of the code that came before it: > > if ( is_numeric($_GET['id'] ?? 0 ) ) { > $id = intvar($_GET['id'] ?? 0); > > $user = getUser($id): > > } > > I'm not sure what the point of this example is. You've just cast to int, so the assertion can never fail, and if getUser has a type hint, it's about to make that assertion anyway. Regards, -- Rowan Tommins [IMSoP] --000000000000b19dc4059567c948--