Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:77595 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 92368 invoked from network); 24 Sep 2014 21:18:07 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 24 Sep 2014 21:18:07 -0000 Authentication-Results: pb1.pair.com header.from=rowan.collins@gmail.com; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=rowan.collins@gmail.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 74.125.82.46 as permitted sender) X-PHP-List-Original-Sender: rowan.collins@gmail.com X-Host-Fingerprint: 74.125.82.46 mail-wg0-f46.google.com Received: from [74.125.82.46] ([74.125.82.46:48775] helo=mail-wg0-f46.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 96/06-57585-E0533245 for ; Wed, 24 Sep 2014 17:18:07 -0400 Received: by mail-wg0-f46.google.com with SMTP id a1so6591381wgh.5 for ; Wed, 24 Sep 2014 14:18:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=message-id:date:from:user-agent:mime-version:to:subject:references :in-reply-to:content-type:content-transfer-encoding; bh=Q0H3kvYvG5NwM8zdYr5HrW/S8/HEJk5WZQe2T4A2f1E=; b=xcnW+FhSkyVR78fa7uWehSRDIRiNrk+tWyrrgig77YSfY9H/rVPnVe4pQppzSozxi9 yTsttKw7a5gWMiDktiU+nlblhO88z9XxJ2ZeAOsgrw+p7k1vEvk6Dh3W8aYBrCl+087o qmLCfmqa0dUEdSFx1b9qXCIhQsKEveQ1vHHJKLoh5h7fZ/IR9GAtcqA5x+L9GNtAMUzP zXx2vOpQ6kP/q/LJ8MuUGkYjgqyX6YqRVmNkgKZRWEO4z4D5k3gBgNvlVvqnsU8clpIo NhoZRmrhDPhgdy51bE7dYdhD76hTmryBiYBVrA4buFOS5bX4ZYYEw2ca2Z86detOXxCf suPg== X-Received: by 10.180.89.230 with SMTP id br6mr14613787wib.16.1411593483547; Wed, 24 Sep 2014 14:18:03 -0700 (PDT) Received: from [192.168.0.2] (cpc68956-brig15-2-0-cust215.3-3.cable.virginm.net. [82.6.24.216]) by mx.google.com with ESMTPSA id ka3sm486622wjc.3.2014.09.24.14.18.02 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Wed, 24 Sep 2014 14:18:02 -0700 (PDT) Message-ID: <54233506.8080102@gmail.com> Date: Wed, 24 Sep 2014 22:17:58 +0100 User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:24.0) Gecko/20100101 Thunderbird/24.6.0 MIME-Version: 1.0 To: internals@lists.php.net References: In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Subject: Re: [PHP-DEV] Little switch improvement: get the used variable From: rowan.collins@gmail.com (Rowan Collins) On 23/09/2014 08:29, Sanford Whiteman wrote: >> The `get_the_used_switch_variable()` is just a placeholder, name can be >> changed to something natural...maybe a constant. > > I feel this has diminished utility once you consider that the "switch > variable" is actually an expression and could well include multiple > $variables. Plus there's also the pattern switch(true) { } where the > interesting variables appear in case statements. Hard for me to see > the justification, but maybe I'm missing it. My $0.02... Surely that makes it *more* useful - the switch statement never contains "multiple variables", it contains a single expression, the exact value of which is not discoverable to the programmer. A key property of switch statements is that the expression is evaluated only once, so the engine is already storing this value, it just needs to be put somewhere accessible. Perhaps rather than a magic function or constant, though, the switch statement could be extended with an "as" argument, which would store the evaluated expression into a normal variable, allowing nesting, and easier optimisation of the engine where the feature isn't used. Thus you could write this: switch( some_expression() as $switch_value ){ case 1: do_something(); break; //... default: throw new Exception('Undefined input: ' . $switch_value); break; } Which would be shorthand for: $switch_value = some_expression(); switch( $switch_value ){ ... As it happens, I've been pondering my own proposed extension to switch(), after some of the discussion that came up from the standardisation. Currently, switch always uses a "loose" comparison (==), so cannot distinguish between "case 3" and "case 3.0". Occasionally, it would be nice to switch on a strict comparison (===); but then I thought, why stop there? What if you could use switch with any comparison operator? My idea is to give the switch() statement an optional "use" clause (since that's an existing keyword) containing a single comparison operator, like so: switch ( $number ) use ( === ) { case 3: // ... break; case 3.0: // No longer unreachable! :) break; } But also: switch ( $age ) use ( < ) { case 2: $type = 'infant'; break; case 18: $type = 'child'; break; default: $type = 'adult'; } This would work well in combination with the "as" keyword: switch ( calculate_age($birth_date, $departure_date) as $age_at_departure ) use ( < ) { case 2: $type = 'infant'; $infants++; break; case 18: $type = 'child'; $child_ages[] = $age_at_departure; break; default: $type = 'adult'; $adults++; } As well as comparison operators, instanceOf might also be useful: switch ( $product ) use ( instanceOf ) { case ProductInterfaces\Flight: // ... break; case ProductInterfaces\Accomm: // ... break; default: // ... } Bitwise operators might be interesting too, but then you're into more complex implementation, because you've got to evaluate the operator and cast the result to boolean before evaluating the case. So the restriction should probably be "any binary operator which evaluates to a boolean result". Thoughts? -- Rowan Collins [IMSoP]