Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:77626 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 67331 invoked from network); 25 Sep 2014 11:12:25 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 25 Sep 2014 11:12:25 -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.182 as permitted sender) X-PHP-List-Original-Sender: rowan.collins@gmail.com X-Host-Fingerprint: 74.125.82.182 mail-we0-f182.google.com Received: from [74.125.82.182] ([74.125.82.182:63391] helo=mail-we0-f182.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id A9/20-00789-898F3245 for ; Thu, 25 Sep 2014 07:12:25 -0400 Received: by mail-we0-f182.google.com with SMTP id u57so5905712wes.27 for ; Thu, 25 Sep 2014 04:12:21 -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=6T+BK8r7/qceg34rrMGbvbH5zM9xi3cV9t0vf9dJxQU=; b=TB1rZFPBn8fhZ6I5WSpDcl5SqPe4imrztgsMhEYz1v2WtpXsgBnpDnEXnrd1gwJsWo TlSdA7twcZskIRToZPULD0YO1Ck+QCiBi4Z1vPzquXxNPaSzRZ5VrZcTarBIT1otIwzq LES0sc4/kKqRHTitqJYQPns9lPY1eZhszSYSCurndHx6KcrFFvvMKxw2ESjG8I8bLabZ 3AFarZUnXZNkYKLrCiDTsuOkI0Dalx74cg7okVOTQh3vmmTpP+HXK7eRwAWIPHKWal8E kZxcFBa9S8bIV21PDzImdH0LTWQIG9/5XtVPp+95xy+OhMr4KoDG80TYN16gj7CaO3Yi BtFw== X-Received: by 10.180.75.49 with SMTP id z17mr37316143wiv.3.1411643541693; Thu, 25 Sep 2014 04:12:21 -0700 (PDT) Received: from [192.168.0.177] ([62.189.198.114]) by mx.google.com with ESMTPSA id iy10sm2818769wic.5.2014.09.25.04.12.19 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Thu, 25 Sep 2014 04:12:21 -0700 (PDT) Message-ID: <5423F892.5010506@gmail.com> Date: Thu, 25 Sep 2014 12:12:18 +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: <54233506.8080102@gmail.com> <54233932.8090102@gmail.com> <5423DBF7.8020402@gmail.com> <4ED7146272E04A47B986ED49E771E347BBF5BA23B8@Ikarus.ameusgmbh.intern> <5423EEF9.8070000@gmail.com> <4ED7146272E04A47B986ED49E771E347BBF5BA23BD@Ikarus.ameusgmbh.intern> In-Reply-To: <4ED7146272E04A47B986ED49E771E347BBF5BA23BD@Ikarus.ameusgmbh.intern> 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) Christian Stoller wrote (on 25/09/2014): > From: Rowan Collins [mailto:rowan.collins@gmail.com], Sent: Thursday, September 25, 2014 12:31 PM >> Sorry, I was talking about this bit: >> >>> 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 >> See my earlier e-mail for examples and further details. Maybe I should >> have given it its own thread so it was more visible; the idea has been >> brewing for a while, I just thought this thread was a convenient place >> to mention. > Ah, okay, sorry. > What do you think about that: > > $value = 3; > switch (true) { > case 3.0 === $value: break; > case 3 < $value: break; > case 3 > $value: break; > } > > Christian When mixing operators like that, it makes sense to use that style - although if you're not using fallthrough, there is little advantage over if/elseif/else: $value = 3; if ( 3.0 === $value ) { } elseif ( case 3 < $value ) { } elseif ( case 3 > $value ) { } At the end of the day, all control structures are glorified conditional jumps, and most switch statements are glorified if/else blocks. To take one of my examples from last night, you could write: if ( $product instanceOf ProductInterfaces\Flight ) { // ... } elseif ( $product instanceOf ProductInterfaces\Accomm ) { // ... } else { // ... } Clearly, this works, as would the same thing using switch(true) and case instead of if/elseif, but the "$product instanceOf" has to be copied into every clause, and someone reading the code cannot assume that all the clauses are performing the same test. In my proposed version, the intent is expressed more clearly, and there is less risk of a bug being introduced by incorrect copying or refactoring: switch ( $product ) use ( instanceOf ) { case ProductInterfaces\Flight: // ... break; case ProductInterfaces\Accomm: // ... break; default: // ... } That's the idea anyway. -- Rowan Collins [IMSoP]