[RFC] Allow "use( $longname as $alias)" syntax for closures declaration

php.internals

Laruence

14 years ago
Hi: I have made a RFC to allow user use T_AS in the closure declaration, like: function () use($long as $l, &$long as $r) { } here is the RFC: https://wiki.php.net/rfc/useas any ideas? thanks
-- Laruence  Xinchen Hui http://www.laruence.com/

Matthew Weier O'Phinney

14 years ago
On 2012-04-12, Laruence <laruence@php.net> wrote:
> I have made a RFC to allow user use T_AS in the closure declaration, like: > > function () use($long as $l, &$long as $r) { > } > > here is the RFC: https://wiki.php.net/rfc/useas > > any ideas? thanks
I really like this, as it brings a symmetry to how "use" is used when importing classes/namespaces; I've often attempted to use "as" within my closure use statements, only to get burned.
-- Matthew Weier O'Phinney Project Lead | matthew@zend.com Zend Framework | http://framework.zend.com/ PGP key: http://framework.zend.com/zf-matthew-pgp-key.asc

Alexandr Verbitsky

14 years ago
On 04/12/2012 06:38 PM, Laruence wrote:
> Hi: > I have made a RFC to allow user use T_AS in the closure declaration, like: > > function () use($long as $l,&$long as $r) { > } > > here is the RFC: https://wiki.php.net/rfc/useas > > any ideas? thanks >
Closure is a functional literal that can pull with itself all variables visible in current scope where it was created. For my opinion using keyword 'use' is not necessary and i would have removed it. If you need different name of variable maybe you need anonymous function.

Laruence

14 years ago
On Fri, Apr 13, 2012 at 4:10 PM, Verbitsky Alexander <verbitsky_alexandr@mail.by> wrote:
> On 04/12/2012 06:38 PM, Laruence wrote: >> >> Hi: >>      I have made a RFC to allow user use T_AS in the closure declaration, >> like: >> >>      function () use($long as $l,&$long as $r) { >> >>      } >> >>      here is the RFC: https://wiki.php.net/rfc/useas >> >>      any ideas?  thanks >> > Closure is a functional literal that can pull with itself all variables > visible in current scope where it was created.
Hi: you mean dup a EG(active_symbol_table)? it's too expensive, IMO. thanks
> For my opinion using keyword > 'use' is not necessary and i would have removed it. If you need different > name of variable maybe you need anonymous function. > > > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: http://www.php.net/unsub.php >
-- Laruence  Xinchen Hui http://www.laruence.com/

Yasuo Ohgaki

14 years ago
Hi, 2012/4/13 Verbitsky Alexander <verbitsky_alexandr@mail.by>:
> On 04/12/2012 06:38 PM, Laruence wrote: >> >> Hi: >>      I have made a RFC to allow user use T_AS in the closure declaration, >> like: >> >>      function () use($long as $l,&$long as $r) { >> >>      } >> >>      here is the RFC: https://wiki.php.net/rfc/useas >> >>      any ideas?  thanks >> > Closure is a functional literal that can pull with itself all variables > visible in current scope where it was created. For my opinion using keyword > 'use' is not necessary and i would have removed it. If you need different > name of variable maybe you need anonymous function. >
You might be used to other language's scoping, but this is the way PHP works. Named function cannot do this, but anyway function FUNC($a, $b, $c) uses ($d, $e, $f) {} is like function FUNC($a, $b, $c) { global $d, $e, $f; } I would rather have this. function FUNC($a, $b, $c) uses ($d, $e, $f) {} Then it would be consistent with anonymous function. There are methods, so we should be careful though. Regards,
-- Yasuo Ohgaki yohgaki@ohgaki.net

Charlie Somerville

14 years ago
I'm at a bit of a loss as to why Laruence is claiming that allowing closures to implicitly access variables in the outer scope requires duplicating the symbol table. Is there any technical reason why it's not possible for scopes to retain a pointer to their parent scopes so variables can be looked up that way? On Apr 13, 2012 6:20 PM, "Yasuo Ohgaki" <yohgaki@ohgaki.net> wrote:

Stas Malyshev

14 years ago
Hi!
> I'm at a bit of a loss as to why Laruence is claiming that allowing > closures to implicitly access variables in the outer scope requires > duplicating the symbol table.
Because variables need to be stored somewhere after the parent function exits.
> Is there any technical reason why it's not possible for scopes to retain a > pointer to their parent scopes so variables can be looked up that way?
Because the parent scope will be gone by the time closure is called. Unless we retain a copy of it - which in most cases is very expensive and impractical - you usually don't need all variables from parent scope - you need 1-2 of them, keeping all of them linked to the closure - and thus not freed until closure ceases to exist - would be very expensive. Declaring shared variables explicitly is a trade-off allowing you to not keep all the variables in case you do not need them.
-- Stanislav Malyshev, Software Architect SugarCRM: http://www.sugarcrm.com/ (408)454-6900 ext. 227

Alexandr Verbitsky

14 years ago
On 04/13/2012 11:36 AM, Stas Malyshev wrote:
> Hi! > >> I'm at a bit of a loss as to why Laruence is claiming that allowing >> closures to implicitly access variables in the outer scope requires >> duplicating the symbol table. > Because variables need to be stored somewhere after the parent function > exits. > >> Is there any technical reason why it's not possible for scopes to retain a >> pointer to their parent scopes so variables can be looked up that way? > Because the parent scope will be gone by the time closure is called. > Unless we retain a copy of it - which in most cases is very expensive > and impractical - you usually don't need all variables from parent scope > - you need 1-2 of them, keeping all of them linked to the closure - and > thus not freed until closure ceases to exist - would be very expensive. > Declaring shared variables explicitly is a trade-off allowing you to not > keep all the variables in case you do not need them.
I am not sure but it is too expensive only for memory. I don't think that current scope will be very big and operation for copying it very slowing because we work with memory (correct me if i not right). Often we don't to know what variables will be needed in future. And seems to me that can to detect which variables bound in closure and copy only it. Many people use PHP not because it very fast and because it very simple to use. Syntax must be more clear for support. And using additional keyword for a bound with variables it's not doing it. At now this is there should not alter of course. What really needed is 'named parameters' For example function some_action($var1 = 0, $var2 = 0) ... If need pass only second parameter i need to do following some_action(0, $b) ... Instead of some_action($var2 = $b) ...

Stas Malyshev

14 years ago
Hi!
> I am not sure but it is too expensive only for memory. I don't think > that current scope will be very big and operation for copying it very
That depends on the scope, it can be very big - e.g. global scope. But more important is not that it is big by itself, but that it retains variables that are otherwise to be freed and thus increases overall memory usage.
> we don't to know what variables will be needed in future. And seems to > me that can to detect which variables bound in closure and copy only it.
If you know any way to do it, please tell. Please account for $$var, etc.
> Many people use PHP not because it very fast and because it very simple > to use. Syntax must be more clear for support. And using additional > keyword for a bound with variables it's not doing it.
I don't think it's very complex syntax - you just specify the variables. It takes 1 minute to read the manual and you know how to do it forever. It's not the rocket science to write use($a, $b).
> At now this is there should not alter of course. What really needed is > 'named parameters'
That's entirely different topic.
-- Stanislav Malyshev, Software Architect SugarCRM: http://www.sugarcrm.com/ (408)454-6900 ext. 227

Christopher Jones

14 years ago
On 04/13/2012 08:49 AM, Stas Malyshev wrote:
>> At now this is there should not alter of course. What really needed is >> 'named parameters' > > That's entirely different topic. >
https://wiki.php.net/rfc/namedparameters
-- Email: christopher.jones@oracle.com Tel: +1 650 506 8630 Blog: http://blogs.oracle.com/opal/

Yasuo Ohgaki

14 years ago
2012/4/13 Yasuo Ohgaki <yohgaki@ohgaki.net>:
> > I would rather have this. > function FUNC($a, $b, $c) uses ($d, $e, $f) {} > Then it would be consistent with anonymous function.
Oops. It became consistent, but there would be no use with PHP. As it cannot pollute global vars.
-- Yasuo Ohgaki yohgaki@ohgaki.net

Laruence

14 years ago
On Fri, Apr 13, 2012 at 4:19 PM, Yasuo Ohgaki <yohgaki@ohgaki.net> wrote:
> Hi, > > 2012/4/13 Verbitsky Alexander <verbitsky_alexandr@mail.by>: >> On 04/12/2012 06:38 PM, Laruence wrote: >>> >>> Hi: >>>      I have made a RFC to allow user use T_AS in the closure declaration, >>> like: >>> >>>      function () use($long as $l,&$long as $r) { >>> >>>      } >>> >>>      here is the RFC: https://wiki.php.net/rfc/useas >>> >>>      any ideas?  thanks >>> >> Closure is a functional literal that can pull with itself all variables >> visible in current scope where it was created. For my opinion using keyword >> 'use' is not necessary and i would have removed it. If you need different >> name of variable maybe you need anonymous function. >> > > You might be used to other language's scoping, but > this is the way PHP works.
closure was not the way PHP works. but now, it has be introduced in PHP
> > Named function cannot do this, but anyway > function FUNC($a, $b, $c) uses ($d, $e, $f) {} > is like > function FUNC($a, $b, $c) { global $d, $e, $f; }
they do are different, previous is a lexical variable. that means it equal to the value when the closure is created. the latter is the value when it was called. thanks
> > I would rather have this. > function FUNC($a, $b, $c) uses ($d, $e, $f) {} > Then it would be consistent with anonymous function. > > There are methods, so we should be careful though. > > Regards, > > -- > Yasuo Ohgaki > yohgaki@ohgaki.net > > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: http://www.php.net/unsub.php >
-- Laruence  Xinchen Hui http://www.laruence.com/

Yasuo Ohgaki

14 years ago
Hi, 2012/4/13 Laruence <laruence@php.net>:
> On Fri, Apr 13, 2012 at 4:19 PM, Yasuo Ohgaki <yohgaki@ohgaki.net> wrote: >> Hi, >> >> 2012/4/13 Verbitsky Alexander <verbitsky_alexandr@mail.by>: >>> On 04/12/2012 06:38 PM, Laruence wrote: >>>> >>>> Hi: >>>>      I have made a RFC to allow user use T_AS in the closure declaration, >>>> like: >>>> >>>>      function () use($long as $l,&$long as $r) { >>>> >>>>      } >>>> >>>>      here is the RFC: https://wiki.php.net/rfc/useas >>>> >>>>      any ideas?  thanks >>>> >>> Closure is a functional literal that can pull with itself all variables >>> visible in current scope where it was created. For my opinion using keyword >>> 'use' is not necessary and i would have removed it. If you need different >>> name of variable maybe you need anonymous function. >>> >> >> You might be used to other language's scoping, but >> this is the way PHP works. > closure was not the way PHP works.  but now,  it has be introduced in PHP >> >> Named function cannot do this, but anyway >> function FUNC($a, $b, $c) uses ($d, $e, $f) {} >> is like >> function FUNC($a, $b, $c) { global $d, $e, $f; } > > they do are different,  previous is a lexical variable.  that means it > equal to the value when the closure is created. > > the latter is the value when it was called.
Thanks for correction. I should have mentioned that. It's only similar that these are importing outside vars into current scope. Importing is the same, but scope is not. I wander if there are any good named function usage that imports current scope vars. Regards,
-- Yasuo Ohgaki yohgaki@ohgaki.net