Re: Suggestion: echo function(var)[0];

From: Date: Mon, 07 Jun 2010 08:07:12 +0000
Subject: Re: Suggestion: echo function(var)[0];
References: 1 2 3 4 5 6  Groups: php.internals 
Request: Send a blank email to internals+get-48668@lists.php.net to get a copy of this message
Sorry for the ambiguity in my original message, and thanks to Tig for PM-ing me about that. To clarify, I support the syntax for: > echo function(var)[0]; and I believe this syntax: > $tmp = getimagesize('./path/to/image'); > echo $tmp[1]; to be awkward and inconvenient. Furthermore, it feels like an unrefined edge in the PHP syntax because in this case $tmp is a completely throwaway variable. While coding I had always wished PHP could have "echo function(var)[0]" and would like to help make this possible. Are there any good reasons currently why the RFCs for this aren't getting accepted? - Harrison On Sat, Jun 5, 2010 at 6:47 PM, Harrison Yuan <newrevolutions@gmail.com> wrote: > > I don't understand what is holding PHP back from having this syntax. > > Tig said: > <? >  $tmp = getimagesize('./path/to/image'); >  echo $tmp[1]; > ?> > > The need to assign the trivial variable $tmp first is completely arbitrary. Is it not a design > goal somewhere that languages should allow the greatest degree of literal expression possible, > consistent with existing syntax & semantics rules? > > I am new here but would be interested in helping with a patch (+ hopefully another RFC) for > this. What exactly are the specific arguments against array dereferencing? > > - Harrison > > On Fri, Jun 4, 2010 at 4:19 PM, mathieu.suen <mathieu.suen@easyflirt.com> wrote: >> >> On 06/04/2010 10:00 AM, Richard Quadling wrote: >>> >>> On 4 June 2010 08:18, mathieu.suen<mathieu.suen@easyflirt.com>  wrote: >>> >>>> >>>> Hi >>>> >>>> Why not something more generic. >>>> Someone could think of a ValueNode. >>>> >>>> Then it could be use for object, array, any primitive type ... >>>> >>>> I will take the ValueNode as a non terminal grammar node. >>>> So first we could do that: >>>> >>>> ValueNode->method(); >>>> ValueNode::sMethod(); >>>> ValueNode[]; >>>> foo(ValueNode); >>>> echo ValueNode; >>>> $e = ValueNode; >>>> ... >>>> >>>> And a ValueNode could be define as: >>>> >>>> ValueNode : >>>> >>>>      NewStatement >>>>    | FunctionCall >>>>    | PrimitiveValue >>>>    | '(' ValueNode ')' >>>>    | ...; >>>> >>>> This would allow all this syntax: >>>> >>>> (new A())->foo(); >>>> foo(new A()); >>>> foo->bar()[1]; >>>> foo()[5]; >>>> >>>> and many others. >>>> >>>> >>>> On 06/04/2010 03:19 AM, Kalle Sommer Nielsen wrote: >>>> >>>>> >>>>> Hi Tig >>>>> >>>>> 2010/6/4 Tig<tigger.on@gmail.com>: >>>>> >>>>> >>>>>> >>>>>> Would be at all possible to implement this kind of shortcut? >>>>>> >>>>>> >>>>> >>>>> Its called array-dereferencing and it was proposed countless times, >>>>> including by myself. There is an RFC for this[1] and it was planned on >>>>> the old PHP6 todo at the PDT[2]. >>>>> >>>>> [1] >>>>> http://wiki.php.net/rfc/functionarraydereferencing >>>>> [2] >>>>> http://wiki.php.net/summits/pdmnotesmay09#php_6 (see point #13) >>>>> >>>>> >>>>> >>>> >>>> --Mathieu Suen >>>> >>>> >>>> >>>> >>>> >>> >>> Not an expert in this area, but does this mechanism limit you to using >>> single dimensional arrays? >>> >>> >> >> Of course not as soon as you add array access to a ValueNode >> >> ValueNode >> >>    .. >>    | ArrayAccess >>    ...; >> >> This is very simple and it can refactor greatly the parser. >> >> -- Mathieu Suen >> >> >> >

« previous php.internals (#48668) next »