Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:77681 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 16020 invoked from network); 26 Sep 2014 16:11:42 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 26 Sep 2014 16:11:42 -0000 Authentication-Results: pb1.pair.com smtp.mail=rowan.collins@gmail.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=rowan.collins@gmail.com; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 74.125.82.170 as permitted sender) X-PHP-List-Original-Sender: rowan.collins@gmail.com X-Host-Fingerprint: 74.125.82.170 mail-we0-f170.google.com Received: from [74.125.82.170] ([74.125.82.170:41398] helo=mail-we0-f170.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id C9/62-02658-D3095245 for ; Fri, 26 Sep 2014 12:11:41 -0400 Received: by mail-we0-f170.google.com with SMTP id q58so364454wes.1 for ; Fri, 26 Sep 2014 09:11:38 -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=EUlvcvVmZezYfgrIBG5b1L1XG5XOD0EKrADOdwCc3MM=; b=z/FHanubDS2viKQJZXq2UMMTseT7VX8oCg5HCo8hM86xvuKx1ieVph7k2882VGiQ9D vLW8JI3uxjZmxCQGbCiQlAoT1rMdsh28nH7/EA0INX5mM8f7ALWzeLl8YAzSlzVhjCgy FjfmzZ6gxARWEqF64yY1fhW9NgtVmQkSo+S2J9n8ItO5TYZUfTDTt20a7G0wdiTEYyJC XHSjYMsZeoPtisLHXs6RqFpUWMe4bPWGrom2bkEvtqbCkA7p4gikUaw99y82tjJsuGy4 AHXcm4H7XEMsp87BN8sRgajBtfj/ZxVGXjbRyMPIdQ8h+bmCufLXR5cVkVNK9C+KPTns Mc6Q== X-Received: by 10.194.60.114 with SMTP id g18mr12747660wjr.94.1411747898587; Fri, 26 Sep 2014 09:11:38 -0700 (PDT) Received: from [192.168.0.177] ([62.189.198.114]) by mx.google.com with ESMTPSA id g6sm6676801wjf.13.2014.09.26.09.11.37 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Fri, 26 Sep 2014 09:11:37 -0700 (PDT) Message-ID: <54259039.5020809@gmail.com> Date: Fri, 26 Sep 2014 17:11:37 +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> <094CB804-0F9F-44D4-BCA2-4D7612E92C10@strojny.net> In-Reply-To: <094CB804-0F9F-44D4-BCA2-4D7612E92C10@strojny.net> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Subject: Re: [PHP-DEV] Little switch improvement: get the used variable From: rowan.collins@gmail.com (Rowan Collins) Lars Strojny wrote (on 26/09/2014): > Hi everyone, > > On 24 Sep 2014, at 23:17, Rowan Collins wrote: > >> switch ( $number ) use ( === ) { > [...] >> switch ( $age ) use ( < ) { > [...] >> switch ( calculate_age($birth_date, $departure_date) as $age_at_departure ) use ( < ) { > [...] >> switch ( $product ) use ( instanceOf ) { > All of these examples are trying to reinvent concepts that can be solved with guards, pattern matching and overloading in other languages. I'm not sure how you see these as equivalent. As I mentioned, a switch statement is a glorified if/elseif/else, or when fallthrough is used, a glorified set of if-then-gotos. > fact(N) when N>0 -> > N * fact(N-1); > > fact(0) -> > 1. Without the additional guarantees provided by a purely functional environment, that's really just inverting the function header and if statement. Adding an extra case to that still means repeating the operator, so isn't the same as what I was talking about at all: foo(N) when N>100 -> N ** foo(N-10); foo(N) when N>0 -> N * foo(N-1); foo(0) -> 1. > In Scala one could replicate the instanceof behaviour by defining multiple methods of the same name with different argument types. Or, indeed, in most strongly typed languages; in OOP, polymorphism can be exploited for the same purpose, e.g. using the Visitor Pattern. Again, I'm not sure what this has to do with switch statements, except that overloading and polymorphism can both allow you to factor out *any* if/switch statement, if you so desire. -- Rowan Collins [IMSoP]