Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:102259 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 95567 invoked from network); 14 Jun 2018 14:49:01 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 14 Jun 2018 14:49:01 -0000 Authentication-Results: pb1.pair.com smtp.mail=php@golemon.com; spf=softfail; sender-id=softfail Authentication-Results: pb1.pair.com header.from=php@golemon.com; sender-id=softfail Received-SPF: softfail (pb1.pair.com: domain golemon.com does not designate 74.125.82.65 as permitted sender) X-PHP-List-Original-Sender: php@golemon.com X-Host-Fingerprint: 74.125.82.65 mail-wm0-f65.google.com Received: from [74.125.82.65] ([74.125.82.65:38873] helo=mail-wm0-f65.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id F2/5B-29356-C50822B5 for ; Thu, 14 Jun 2018 10:49:01 -0400 Received: by mail-wm0-f65.google.com with SMTP id 69-v6so12524583wmf.3 for ; Thu, 14 Jun 2018 07:49:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=golemon-com.20150623.gappssmtp.com; s=20150623; h=mime-version:sender:in-reply-to:references:from:date:message-id :subject:to:cc:content-transfer-encoding; bh=LwlmG0SnWiFJ8UXGmL79KJAmro9P6uU5/sA1QXFGZWM=; b=DVspHlgbm4xMYarumLPVGwXgynVR+YxZuFep6UkDBss33GcL9uWAKiwGj8G/k1MIMi QFuZX2ZMh3HG0iNhcKjFzmxbACuF790PYfcBDUlm2oFfffaGfIgbcSM9sVG+GYqcOasP s5acm8d8ZF37YvU2pIILD884RLZ5rGISmwTi7wz+BMO4SkPBTVeh8WOfD3rMX7Xv8NRb R0qJt9Mjl2pW1jXSKz/3RvF/tJsAzRr0rLIwf0NuCA+uevq+3wzY3sSzqPXCGW58N6Ay kcEZU7sFNayfdrq/xc5s+qxsCWDnRajddqxbbcJrgnkATZ5CUnmGfjHNp25poZc2ny0n hgwA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:sender:in-reply-to:references:from :date:message-id:subject:to:cc:content-transfer-encoding; bh=LwlmG0SnWiFJ8UXGmL79KJAmro9P6uU5/sA1QXFGZWM=; b=Wevj/uWq8mmmqED3fG0y8kywcEZzcy+wOJ11/bz6Tlh6n9WNA1LyYOJf0ZVC3UKpvR 9BAKJ23WAR1TO+SsbyKXFYSnxAePlykB0p6M9ipfkVhl3yXC3LPRS8Io5Hr8oHFEPSnl Oq8uo9OcAtHkOLOntFiDCPX8zxSgKxAf+Tem5AiJUGu9bKjKbi3adPaEVGX2G+YsVIFc NUuobYXyGTYxcsBMb3/0h0i1wXD+Y/RQYsBBLpoTQJvWE0NeJbIsMHl8PCenqvaIHfpW KX2tukApIrdZeaGQWVQdJLkPBqoC2zZoBxsKXi3G5jTSDbGQzMYYASVmSKABR+6kBSwi wX8Q== X-Gm-Message-State: APt69E0oiRXAqSHNMvn2tYvtNI90Rpl8H8PWX2II2YvudVvK+z9SmS3u DqlCaPCSp6mE2YnC668zbjCkwFFrk1xhJlC64dTohA== X-Google-Smtp-Source: ADUXVKIp37uii3t0WT2i+HC5vzM0q45gbZoU2/HhLxjrzwNpf6VdWs+KfH74Z8XnvSf7ah0wEihE/juzA5qolzsG/fk= X-Received: by 2002:a50:8f23:: with SMTP id 32-v6mr2938496edy.192.1528987737988; Thu, 14 Jun 2018 07:48:57 -0700 (PDT) MIME-Version: 1.0 Sender: php@golemon.com Received: by 2002:a50:8625:0:0:0:0:0 with HTTP; Thu, 14 Jun 2018 07:48:57 -0700 (PDT) X-Originating-IP: [98.116.190.42] In-Reply-To: References: Date: Thu, 14 Jun 2018 10:48:57 -0400 X-Google-Sender-Auth: OK09lja2oVXO7YWX91shUWNqyQk Message-ID: To: Nikita Popov Cc: PHP internals Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Subject: Re: [PHP-DEV] Strict switch statements From: pollita@php.net (Sara Golemon) On Thu, Jun 14, 2018 at 4:35 AM, Nikita Popov wrote: > I like the general idea here (switch with strict type comparison), but no= t > super fond of the particular syntax and implementation. > No arguments there. What's presented is "best I could come up in the bath"= . :) > 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 "switc= h > strict ($expr) {}" or "switch (strict $expr) {}" or "switch ($expr) stric= t > {}" or "switch ($expr) { strict; }" or whatever would be preferable in th= at > case. > Agree that it's more likely to be all-or-not within a switch block. If I could step through my thinking in putting it on the case statement however, I applied two starting rules: 1. Avoid adding new reserved symbols/keywords. 2. Try to make it read naturally. I actually did consider Rowan's suggestion `switch (expr) use (=3D=3D=3D) {}`, but I didn't feel like it satisfied #2 well. Instead, I chose to think of the T_CASE as a placeholder for the switch expression. If you visually substitute the expression where the 'case' is at, then this: switch ($x) { case =3D=3D=3D 1: case =3D=3D=3D 2: } Could be read as: switch (...) { $x =3D=3D=3D 1: $x =3D=3D=3D 2: } Not saying that's the only route, just walking through how I would imaging it reading to new eyes. > Additionally, switch has the issue of fall-through behavior, which is > somewhat unexpected and error prone to many people. It might make sense t= o > introduce an entirely new "match" statement that conforms a bit more with > how switch-like strictures are implemented nowadays. That is, something l= ike > > match ($expr) { > "foo" =3D> {...}, > "bar" | "baz" =3D> {...}, > } > > or similar. This might need some more design work to ensure forward > compatibility with potential future algebraic datatypes etc. > That's certainly more ambitious, but since we're just casually talking, then let's explore it. I like the idea of a richer syntax for matching that goes beyond simple equality/identicality, but we'll need to think very carefully about interactions (for example, in your example, is `"bar" | "baz"` a bitwise binary OR?) What might default look like here? I guess we still reuse the default keyword. My nit-pick about the "match" keyword is that I (personally, and this is probably minority) think of this like regex related. I'll be honest, I didn't realize the fall-through behavior was a learning curve issue. This block scoping would address that, but I wonder if we could make use of break/continue. The former's meaning being obvious enough, the latter allowing us to either fall through to the next case or possibly continuing evaluating conditionals. =F0=9F=A4=94 -Sara