Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:54384 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 3315 invoked from network); 4 Aug 2011 20:21:01 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 4 Aug 2011 20:21:01 -0000 Authentication-Results: pb1.pair.com smtp.mail=sebastian.krebs.berlin@googlemail.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=sebastian.krebs.berlin@googlemail.com; sender-id=pass Received-SPF: pass (pb1.pair.com: domain googlemail.com designates 209.85.215.42 as permitted sender) X-PHP-List-Original-Sender: sebastian.krebs.berlin@googlemail.com X-Host-Fingerprint: 209.85.215.42 mail-ew0-f42.google.com Received: from [209.85.215.42] ([209.85.215.42:55648] helo=mail-ew0-f42.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id D3/F1-24235-C2FFA3E4 for ; Thu, 04 Aug 2011 16:21:01 -0400 Received: by ewy2 with SMTP id 2so1200179ewy.29 for ; Thu, 04 Aug 2011 13:20:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=googlemail.com; s=gamma; h=message-id:date:from:user-agent:mime-version:to:subject:references :in-reply-to:content-type:content-transfer-encoding; bh=IakA+UAJanoBYNEw1WS2RrJ7Y890QmCgZNOV3S4zuds=; b=EsPA6JcCpx2HA1DIjwwlD06kvBROHyraJb6ERhXG1r54Qw+eqsTA7X9mOsf6HfF9J0 VMWYOCgPY5/2Pt+kbrMgZFZHa0uXc0gxpc5sFuD62rxZrKtZdsdMkPH4j1X/71bofA+k W0EX7a+UZOspVgQhb9qRBr+g82PW4O8vP5yNA= Received: by 10.204.142.144 with SMTP id q16mr412671bku.147.1312489257501; Thu, 04 Aug 2011 13:20:57 -0700 (PDT) Received: from [192.168.24.13] (91-64-205-130-dynip.superkabel.de [91.64.205.130]) by mx.google.com with ESMTPS id z23sm650570bks.43.2011.08.04.13.20.56 (version=SSLv3 cipher=OTHER); Thu, 04 Aug 2011 13:20:56 -0700 (PDT) Message-ID: <4E3AFF27.4050501@googlemail.com> Date: Thu, 04 Aug 2011 22:20:55 +0200 User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2.17) Gecko/20110516 Thunderbird/3.1.10 MIME-Version: 1.0 To: internals@lists.php.net References: <4E3AF896.2090106@officechristmas.co.uk> In-Reply-To: <4E3AF896.2090106@officechristmas.co.uk> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Subject: Re: [PHP-DEV] Re: An implementation of a short syntax for closures From: sebastian.krebs.berlin@googlemail.com (Sebastian Krebs) Just two examples, why I think, this is a not a good idea. Simple typos can produce hard to track errors. Its just confusing $x = 4; $y = ($x);{ return $x*2; } // and $x = 4; $x = ($x) { $y = $x*2; } On 04.08.2011 21:52, Jezz Goodwin wrote: > Hello PHP Internals, > > This is my first message to the list. I've been reading via the archive > since I read the 5.4RC1 announcement. (Keep up the great work guys - I'm > loving the new stuff) > > Up until now I haven't had anything to say that wasn't already being > talked about, but the topic of short hand syntax is of particular > interest to me. > > Before I start, I think I need to make clear that I am in general not a > fan of making cryptic code (ala perl). I tend to use long descriptive > variable names and try to make my code easy to read so that comments > aren't usually necessary. > > Saying that though, I am a big supporter of the new short hand array > syntax. From a grammatical point of view, I see no reason why my code > needs to be scattered with the word 'array'. The simple square brackets > ['a'=>'b'] I find just as easy to understand and they don't confuse me. > (unlike something along the lines of regex, that can easily confuse). > > I see the desire to wanting to remove the word function as a similar > issue. When you use inline functions a lot, your code is filled with > lots of the word 'function'. This word doesn't necessarily need to be > there for your code still to make sense. > > Also, on a similar vein of thought, if your function is only doing > something small, why do we need to put the word 'return' in there? > > I have been thinking of what syntax(s) could work to shorten the code > but not to make the code confusing. I am splitting up my ideas in to > separate sections. I think that if a short-hand function syntax is to be > accepted, it should be useful in many circumstances - eg, it shouldn't > necessarily be limited to one liners. > > > > Okay, here is a base example: > > $modify = function( $x )use( $my_constant ){ return $x + $my_constant; }; > > > 1) REMOVING THE WORD 'function': > > One solution is to simply remove the word function completely: > > $modify = ( $x )use($my_constant ){ return $x + $my_constant; }; > > On first glance, to me, this doesn't look like I'm starting to write a > function. I could perhaps be opening a bracket to do some maths. So, > what could be done to make it more obvious? I suggest using a character > to represent function. Personally I like colon: > > $modify = :( $x )use( $my_constant ){ return $x + $my_constant; }; > > Now, I know this doesn't immediately scream function, but it is a > different/specific syntax which should hopefully lead to less confusion. > > > > 2) REMOVING THE WORD 'return': > > Okay, my function is still a bit wordy for what it's doing. How do we > remove return? I quite like the use of => that has been suggested in > previous posts. > > $modify = :( $x )use( $my_constant )=> $x + $my_constant; > > > > 3) REMOVING THE WORD 'use': > > Personally I don't think use is causing a problem, I find it helps the > grammar of this function. If it was to go though, it could be replaced > with something like a colon: > > $modify = :( $x : $my_constant )=> $x + $my_constant; > > Is this going too far? Is it now too cryptic? > > > > 4) REMOVING THE BRACKETS? > > In lots of the examples of short hand closures in other languages there > are no brackets. I think that might be going too far, but perhaps not: > > $modify = : $x : $my_constant => $x + $my_constant; > > Too much! This line simply confuses me! > > > > OTHER EXAMPLES > > So, personally I don't think shortening needs to go as far as 3 or 4. > But, I am a fan of 1 and 2. > > To demonstrate more how this might look, I'm using the code examples > Lazare used in his original post: > > // Mapping: > $a->select( function($x){ return $x->getName(); } ); > > // Filtering: > $a->where( function($x)use($y){ return $x==$y; } ); > > // Currying: > $add = function($x){ return function($y)use($x){ return $x+$y; }; }; > > > > With my examples of shortening, these become: > > // Mapping: > $a->select( :($x)=> $x->getName() ); > > // Filtering: > $a->where( :($x)use($y)=> $x==$y ); > > // Currying: > $add = :($x)=> :($y)use($x)=>$x+$y ; > > > Hmm... I don't like my currying example, it's starting to boggle my > mind. How about: > > $add = :($x)=>( :($y)use($x)=>$x+$y ); > > Much better. > > > > Regardless of all of this, I actually don't find myself using too many > inline functions. (Not to the extent I was writing 'array' all over my > code anyway) - So I can't argue srongly about function shortening making > it's way in to PHP. > > Perhaps once we're all using inline functions on every other line of our > code there'll be a bigger demand for it! > > > Best regards, > Jezz Goodwin > > > > >