# PATCH: anonymous functions in PHP

- Thread: https://externals.io/message/34040
- Original archive: https://news-web.php.net/php.internals/34040
- Started by troels knak-nielsen on 2007-12-15T16:18:48+00:00
- Messages: 67
- RFC: Short Closures 2.0 (Declined) — https://externals.io/rfc/auto-capture-closure.md
- Page 1 of 3: messages 1 to 25 (25 per page, add `?page=N` to the URL)
- Next page: https://externals.io/message/34040.md?page=2
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · RFCs at https://externals.io/rfc.md · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #34040 — troels knak-nielsen — 2007-12-15T16:18:48+00:00

URL: https://externals.io/message/34040#34040

What was the conclusion on Wez' patch from march [1]? The discussion
seemed to steer a bit off, on the discussion of scoping rules, but is
there any reason _not_ to implement Wez' patch in HEAD?
Even if it doesn't entirely replace create_function, it would be nice
to have as a compile-time alternative.

[1] http://groups.google.com/group/mailing.www.php-dev/browse_thread/thread/a2c3296dc791369a/075209b288cb28de

-- 
troels

---

## #34044 — Stanislav Malyshev — 2007-12-16T06:57:40+00:00

URL: https://externals.io/message/34040#34044 · In reply to: #34040

I think the problem there is that this syntax wouldn't support external 
variables, and without them there's not much difference between that and 
create_function.

troels knak-nielsen wrote:
> What was the conclusion on Wez' patch from march [1]? The discussion
> seemed to steer a bit off, on the discussion of scoping rules, but is
> there any reason _not_ to implement Wez' patch in HEAD?
> Even if it doesn't entirely replace create_function, it would be nice
> to have as a compile-time alternative.
> 
> [1] http://groups.google.com/group/mailing.www.php-dev/browse_thread/thread/a2c3296dc791369a/075209b288cb28de
> 

-- 
Stanislav Malyshev, Zend Software Architect
stas@zend.com   http://www.zend.com/
(408)253-8829   MSN: stas@zend.com

---

## #34045 — troels knak-nielsen — 2007-12-16T11:24:17+00:00

URL: https://externals.io/message/34040#34045 · In reply to: #34044

Actually it's the opposite. With create_function, you can bind
variables, by marshalling them to a string and embed them in the
function-definition. With a static syntax, this isn't possible.
However, this is only a problem, when you need to bind variables,
which isn't always the case and even then, there are other ways to
bind variables in runtime.
So it's different from create_function, in that it doesn't allow
binding of variables, but I don't see why that should stop us from
implementing, considering the benefits it gives in readability and
debugging.

--
troels


> I think the problem there is that this syntax wouldn't support external
> variables, and without them there's not much difference between that and
> create_function.
>
>
> troels knak-nielsen wrote:
> > […]
>
> --
> Stanislav Malyshev, Zend Software Architect
> stas@zend.com   http://www.zend.com/
> (408)253-8829   MSN: stas@zend.com

---

## #34046 — Alexey Zakhlestin — 2007-12-16T14:22:40+00:00

URL: https://externals.io/message/34040#34046 · In reply to: #34044

> I think the problem there is that this syntax wouldn't support external
> variables, and without them there's not much difference between that and
> create_function.

The difference is, that it is compile-time and create_function is run-time

> troels knak-nielsen wrote:
> > […]
>
> --
> Stanislav Malyshev, Zend Software Architect
> stas@zend.com   http://www.zend.com/
> (408)253-8829   MSN: stas@zend.com
>
> --
> 
> 
>
>


-- 
Alexey Zakhlestin
http://blog.milkfarmsoft.com/

---

## #34048 — Stanislav Malyshev — 2007-12-16T19:56:22+00:00

URL: https://externals.io/message/34040#34048 · In reply to: #34045

> Actually it's the opposite. With create_function, you can bind
> variables, by marshalling them to a string and embed them in the

That's not binding. But the problem is, seeing this, one expects 
closure. And it's no closure.

> So it's different from create_function, in that it doesn't allow
> binding of variables, but I don't see why that should stop us from
> implementing, considering the benefits it gives in readability and
> debugging.

I don't see how it'd help anything in debugging.
-- 
Stanislav Malyshev, Zend Software Architect
stas@zend.com   http://www.zend.com/
(408)253-8829   MSN: stas@zend.com

---

## #34051 — Lars Strojny — 2007-12-17T07:44:03+00:00

URL: https://externals.io/message/34040#34051 · In reply to: #34048

Hi,

Am Sonntag, den 16.12.2007, 11:56 -0800 schrieb Stanislav Malyshev:
> > […]
> 
> That's not binding. But the problem is, seeing this, one expects 
> closure. And it's no closure.

Maybe deprecating create_function() and allowing something like that
would be a way to go?
$func = function($arg) {
   return $arg;
}

As it is compile time, the issue with Op-Code caches would/should go
away.

cu, Lars
-- 
Lars Strojny
Senior Software Developer

Neu.de GmbH
Wesselinger Strasse 28
50999 Köln

Eingetragen beim Amtsgericht Köln, HRB 61021
Geschäftsführer: Sven Jan Arndt, Christian Dornhoff

Telefon: 02236-48010-21
Fax:     02236-48010-01
Mobil:   0177-3058654

E-Mail: lstrojny@neu.de
Jabber: lars@mabber.com

---

## #34052 — Lars Strojny — 2007-12-17T07:50:47+00:00

URL: https://externals.io/message/34040#34052 · In reply to: #34051

Hi,

Am Montag, den 17.12.2007, 08:44 +0100 schrieb Lars Strojny:
[...]
> Maybe deprecating create_function() and allowing something like that
> would be a way to go?
> $func = function($arg) {
>    return $arg;
> }

What a stupid and useless remark. Ignore me, sorry.

cu, Lars

---

## #34053 — Alexey Zakhlestin — 2007-12-17T08:20:34+00:00

URL: https://externals.io/message/34040#34053 · In reply to: #34048

> > […]
>
> That's not binding. But the problem is, seeing this, one expects
> closure. And it's no closure.

Well, documentation can include the following written using large font:

Warning: This is not a closure. PHP doesn't have native means for
nested contexts. This construct is just another way of creating usual
function during compile-time. (for creating functions in run-time see
create_function)

-- 
Alexey Zakhlestin
http://blog.milkfarmsoft.com/

---

## #34054 — troels knak-nielsen — 2007-12-17T08:21:07+00:00

URL: https://externals.io/message/34040#34054 · In reply to: #34048

> I don't see how it'd help anything in debugging.

Presumably, a stack trace would now contain file and line number.

> That's not binding. But the problem is, seeing this, one expects
> closure. And it's no closure.

One might expect that, coming from a language with static scope. Then
again, one would also expect global variables in that event. I don't
really see this as a huge show stopper. My experience from Javascript
(Which probably is the language, that most PHP'ers are likeable to
have met anonymous functions in) is, that lesser experienced
programmers, are often surprised by static scope. Even if they have
used Javascript for a while, they have just assumed, that it was
global variables. I think the majority of the users won't miss the
feature and those who do, will be able to appreciate why it isn't
there.

-- 
troels

---

## #34061 — Marcus Boerger — 2007-12-17T13:09:47+00:00

URL: https://externals.io/message/34040#34061 · In reply to: #34046

Hello Alexey,

  and for that reason we should do it! Can't Wez simply apply this?

marcus

Sunday, December 16, 2007, 3:22:40 PM, you wrote:

> 
> > […]

> The difference is, that it is compile-time and create_function is run-time

> > […]


> -- 
> Alexey Zakhlestin
> http://blog.milkfarmsoft.com/




Best regards,
 Marcus

---

## #34064 — Andi Gutmans — 2007-12-17T17:09:50+00:00

URL: https://externals.io/message/34040#34064 · In reply to: #34061

I actually remember there was some good discussion on this and there
were pretty decent reasons for why this patch was not a full solution
and only syntactic sugar, i.e. it didn't do what many expected except
for making the syntax nicer.
Don't have time right now but we should go back and re-read those
discussions before opening a new one. I can probably do it over the next
couple of days. I am not necessary against supporting such a solution
but we need to make sure it's fully baked and that we clear know and
agree on what is and what isn't supported (and that should be documented
and not revisited every time someone comes and complains). Variable
binding is one thing which would likely not make it into such an
implementation unless there's a big shift in the intent of the patch.

We really need to get away from this "let's just commit it" mode on this
list. As you saw with garbage collection, namespaces and other recent
topics a lot of these topics need significantly more work before they
are full baked and ready to actually make it into the code base. The
ad-hoc way of committing new features just doesn't work. 

Andi

> -----Original Message-----
> From: Marcus Boerger [mailto:helly@php.net]
> Sent: Monday, December 17, 2007 5:10 AM
> To: Alexey Zakhlestin; Wez Furlong
> Cc: Stas Malyshev; troels knak-nielsen; internals@lists.php.net
> Subject: Re: [PHP-DEV] Re: PATCH: anonymous functions in PHP
> 
> Hello Alexey,
> 
>   and for that reason we should do it! Can't Wez simply apply this?
> 
> marcus
> 
> Sunday, December 16, 2007, 3:22:40 PM, you wrote:
> 
> > […]
> external
> > […]
that
> and
> > […]
> 
> > […]
> run-time
> 
> > […]
> discussion
> > […]
but
> is
> > […]
> nice
> > […]
> dev/browse_thread/thread/a2c3296dc791369a/075209b288cb28de
> > […]
> 
> 
> > […]
> 
> 
> 
> 
> Best regards,
>  Marcus

---

## #34065 — Stanislav Malyshev — 2007-12-17T17:14:22+00:00

URL: https://externals.io/message/34040#34065 · In reply to: #34061

>   and for that reason we should do it! Can't Wez simply apply this?

I think we saw numerous examples that "commit first, think later" 
approach is not the best one...

-- 
Stanislav Malyshev, Zend Software Architect
stas@zend.com   http://www.zend.com/
(408)253-8829   MSN: stas@zend.com

---

## #34067 — Sebastian Bergmann — 2007-12-17T17:36:59+00:00

URL: https://externals.io/message/34040#34067 · In reply to: #34064

Andi Gutmans wrote:
> We really need to get away from this "let's just commit it" mode on this
> list. As you saw with garbage collection, namespaces and other recent
> topics a lot of these topics need significantly more work before they
> are full baked and ready to actually make it into the code base. The
> ad-hoc way of committing new features just doesn't work. 

 Amen to that. A long time ago we wanted to use text files in an RFC
 directory in CVS to discuss new language features. Maybe we should
 resurrect this idea?

-- 
Sebastian Bergmann                          http://sebastian-bergmann.de/
GnuPG Key: 0xB85B5D69 / 27A7 2B14 09E4 98CD 6277 0E5B 6867 C514 B85B 5D69

---

## #34068 — Stanislav Malyshev — 2007-12-17T18:16:05+00:00

URL: https://externals.io/message/34040#34068 · In reply to: #34053

> Well, documentation can include the following written using large font:
> 
> Warning: This is not a closure. PHP doesn't have native means for
> nested contexts. This construct is just another way of creating usual
> function during compile-time. (for creating functions in run-time see
> create_function)

It doesn't help. Nobody reads the docs if he thinks he understands how 
it should work anyway. Having disclaimer in the manual doesn't do much - 
we are not worried about being sued, we are worried about feature 
looking one way and actually working other way.
-- 
Stanislav Malyshev, Zend Software Architect
stas@zend.com   http://www.zend.com/
(408)253-8829   MSN: stas@zend.com

---

## #34070 — troels knak-nielsen — 2007-12-17T19:37:57+00:00

URL: https://externals.io/message/34040#34070 · In reply to: #34064

> Don't have time right now but we should go back and re-read those
> discussions before opening a new one. I can probably do it over the next
> couple of days. I am not necessary against supporting such a solution
> but we need to make sure it's fully baked and that we clear know and
> agree on what is and what isn't supported (and that should be documented
> and not revisited every time someone comes and complains). Variable
> binding is one thing which would likely not make it into such an
> implementation unless there's a big shift in the intent of the patch.

Allow me to recap then.

Static lambdas (in lack of a better name) and create_function() differ
from each other in that the former happens at compile time, while the
latter happens at run time. The consequence of this is, that you can
bind (primitive) variables to the created function, with
create_function(), but not with static lambdas.
Functionally, create_function() is a variation of eval, which allows
you to create new code at runtime. Static lambda is syntactic sugar
for creating a function in the global scope, without knowing its name
at compile time. Static lambda is more restrictive than
create_function(), because there is no way to share scope between the
inner and the bounding function. This is specific for PHP, because the
language doesn't have static scope. Without going much into details, I
think it's pretty safe to say, that introducing static scope into PHP
is not only undesireable, but also technically impossible. So let's
not spend any time discussing that.

In brief, there are the following benefits of introducing static lambdas:
A1 Better readability.
A2 Syntactic errors happen at compile time, rather than run time.
A3 Better support for debugging/introspection.
A4 Optimisations are possible.

And the following problems:
B1 Users might assume, that lambdas have static scope.
B2 There is some overlap between the application of create_function()
and static lambdas, meaning some level of redundancy.
B3 Static lambdas are less powerful, since there is no way to bind variables.

If I missed any points, please bring them up. Otherwise, this is what
a decision should be based on. (And sorry for pointing out the
obvious, but _not_ making a decision, is a decision too)

Of the cons, B1 is probably the biggest issue. There are (at least)
two considerations to take here; First, how probable is it, that users
will make this assumption? And second, how much confusion will it
cause to those, which it affects?
The first question could perhaps be answered by a survey (Or less
scientifically, by random consensus), but the latter is rather
subjective. I'd like to make the argument, that those who will tend to
suffer most from peculiarities of scoping rules, are the same people,
who are less likely to have encountered static scope in the first
place. The simple rule, that PHP has dynamic scope, should be simple
to understand for anyone who actually knows the difference.

Did I miss anything?

-- 
troels

---

## #34071 — Marcus Boerger — 2007-12-17T19:45:39+00:00

URL: https://externals.io/message/34040#34071 · In reply to: #34065

Hello Stanislav,

  I do not see anything broken here. All I see is discussing a patch to
death just because it doesn't do what everybody wants.

marcus

Monday, December 17, 2007, 6:14:22 PM, you wrote:

> > […]

> I think we saw numerous examples that "commit first, think later" 
> approach is not the best one...

> -- 
> Stanislav Malyshev, Zend Software Architect
> stas@zend.com   http://www.zend.com/
> (408)253-8829   MSN: stas@zend.com




Best regards,
 Marcus

---

## #34072 — Stanislav Malyshev — 2007-12-17T19:46:42+00:00

URL: https://externals.io/message/34040#34072 · In reply to: #34070

> Functionally, create_function() is a variation of eval, which allows
> you to create new code at runtime. Static lambda is syntactic sugar
> for creating a function in the global scope, without knowing its name
> at compile time. Static lambda is more restrictive than

While we are at it, what's wrong with knowing the name? I can see why 
closure can be fun when you can dynamically use outer-scope variables. 
But when you can't, what exactly prevents one from just doing the function?

> two considerations to take here; First, how probable is it, that users
> will make this assumption? And second, how much confusion will it
> cause to those, which it affects?

First, about 100% on the first encounter for any user ever seeing 
closures in any other language. Second, all the confusion possible, like 
"oh, closures! cool! let me do this and that! what, I can't use 
variables?! Are you kidding me?! WTF is it useful for?!"

> place. The simple rule, that PHP has dynamic scope, should be simple
> to understand for anyone who actually knows the difference.

I'm not sure I understand what you mean by "static scope" and "dynamic 
scope", but anyway thing that looks like closure but works like regular 
function definition that isn't - is not a very good idea, IMO.
-- 
Stanislav Malyshev, Zend Software Architect
stas@zend.com   http://www.zend.com/
(408)253-8829   MSN: stas@zend.com

---

## #34073 — Jeff Moore — 2007-12-17T23:41:52+00:00

URL: https://externals.io/message/34040#34073 · In reply to: #34048

Hello,

Reading the prior discussion, I think either $_SCOPE['x'] or the  
lexical $x syntax is fine for accessing local variables in the  
enclosing scope.   But closures also should also support $this and  
static:: when the closure is defined in a method.

I think a solution for closures should add a new variable type.  That  
way, the code can compiled at compile time, while the enclosing  
context (local variables plus $this for methods) can be captured at  
run time in the new variable type.  Also, when the closure value is  
garbage collected, the references to the enclosing scope and object  
instance can also be freed.

Additionally, the callback psuedo-type could be promoted to the new  
type, with the string or array representation converted when  
required.  It wouldn't be necessary, but a callback literal along the  
lines of Array() could also be added to make the conversion  
unnecessary.  I find the following to be easier to understand:

set_error_handler(Callback("foo"));

Than

set_error_handler("foo");

The current syntax would still work because of automatic type  
conversion.

Best Regards,

Jeff

---

## #34074 — Larry Garfield — 2007-12-18T03:30:08+00:00

URL: https://externals.io/message/34040#34074 · In reply to: #34073

Let me see if I'm understanding you correctly, because that sounds like the 
line I'd been thinking along.

----------
function baz() {

  $var = 5;

  $foo = function($a, $b) {
    lexical $var;
    return $a + $b + $var;
  }

  return $foo;
}

$func = baz();

function bar($func) {
  return $func(2, 3);
}

echo bar($foo); // prints 10
----------

Right?  Would it be possible to push the heavy lifting onto the compiler 
rather than runtime in the above case?

I'm assuming that making the function above GC-able would be a herculean task 
at this point, based on previous comments, but I do not actually know myself.


> Hello,
>
> Reading the prior discussion, I think either $_SCOPE['x'] or the
> lexical $x syntax is fine for accessing local variables in the
> enclosing scope.   But closures also should also support $this and
> static:: when the closure is defined in a method.
>
> I think a solution for closures should add a new variable type.  That
> way, the code can compiled at compile time, while the enclosing
> context (local variables plus $this for methods) can be captured at
> run time in the new variable type.  Also, when the closure value is
> garbage collected, the references to the enclosing scope and object
> instance can also be freed.
>
> Additionally, the callback psuedo-type could be promoted to the new
> type, with the string or array representation converted when
> required.  It wouldn't be necessary, but a callback literal along the
> lines of Array() could also be added to make the conversion
> unnecessary.  I find the following to be easier to understand:
>
> set_error_handler(Callback("foo"));
>
> Than
>
> set_error_handler("foo");
>
> The current syntax would still work because of automatic type
> conversion.
>
> Best Regards,
>
> Jeff


-- 
Larry Garfield			AIM: LOLG42
larry@garfieldtech.com		ICQ: 6817012

"If nature has made any one thing less susceptible than all others of 
exclusive property, it is the action of the thinking power called an idea, 
which an individual may exclusively possess as long as he keeps it to 
himself; but the moment it is divulged, it forces itself into the possession 
of every one, and the receiver cannot dispossess himself of it."  -- Thomas 
Jefferson

---

## #34075 — Jeff Moore — 2007-12-18T04:44:37+00:00

URL: https://externals.io/message/34040#34075 · In reply to: #34074

> I'm assuming that making the function above GC-able would be a  
> herculean task
> at this point, based on previous comments, but I do not actually  
> know myself.

Hi Larry,

Let me use a different example than yours.

function getAdder($x) {
   return function ($y) {
     lexical $x;
     return $x + $y;
   }
}

$plusFive = getAdder(5);
$plusTen = getAdder(10);

echo $plusFive(4); // 9
echo $plusTen(7); // 17

If the closure definition (and thus getAddr) returns a string  
representing an anonymous function name, then there is no where to  
hold the enclosing variable $x and no way to know when to free it.   
Notice that in this example, we have two different enclosing contexts,  
one where $x is 5 and one where $x is 10.

If we introduce a new variable type, a new kind of zval, then  
internally, that variable can hold:
1) A reference to the local variables of getAddr.  (here $x)
2) A reference to $this if the closure is declared in a method
3) A reference to the opcodes of the compiled anonymous function.

The opcodes for the closure would be compiled and squirreled away when  
the script is compiled.

When getAddr is executed, a zval would be created that holds the  
reference to the enclosing locals and a reference to the proper  
opcodes.  Note that $plusFive and $plusTen would hold two different  
zvals, one for each invocation of getAddr, but that both would refer  
to the same opcodes structure.

When a codeblock zval is executed, such as at $plusFive(4), the  
enclosing context can be passed to the anonymous function function to  
make the lexical $x statement bind to the proper variable.

When the zval in $plusFive reaches zero references, it can  
subsequently free its references to the local variables of getAddr.

As far as I can see, no herculean garbage collection or reference  
counting is required.

Adding a new kind of zval?  Perhaps that's Herculean.  I don't know.

I hope I've gotten my zval terminology right.

Best Regards,

Jeff

---

## #34077 — Larry Garfield — 2007-12-18T05:41:54+00:00

URL: https://externals.io/message/34040#34077 · In reply to: #34075

> 
> > […]
>
> Hi Larry,
>
> Let me use a different example than yours.
>
> function getAdder($x) {
>    return function ($y) {
>      lexical $x;
>      return $x + $y;
>    }
> }
>
> $plusFive = getAdder(5);
> $plusTen = getAdder(10);
>
> echo $plusFive(4); // 9
> echo $plusTen(7); // 17
>
> If the closure definition (and thus getAddr) returns a string
> representing an anonymous function name, then there is no where to
> hold the enclosing variable $x and no way to know when to free it.
> Notice that in this example, we have two different enclosing contexts,
> one where $x is 5 and one where $x is 10.
>
> If we introduce a new variable type, a new kind of zval, then
> internally, that variable can hold:
> 1) A reference to the local variables of getAddr.  (here $x)
> 2) A reference to $this if the closure is declared in a method
> 3) A reference to the opcodes of the compiled anonymous function.
>
> The opcodes for the closure would be compiled and squirreled away when
> the script is compiled.
>
> When getAddr is executed, a zval would be created that holds the
> reference to the enclosing locals and a reference to the proper
> opcodes.  Note that $plusFive and $plusTen would hold two different
> zvals, one for each invocation of getAddr, but that both would refer
> to the same opcodes structure.

So the compiled code is then single-sourced, much the same way that a class is 
single sourced and objects simply contain references to it.  The Objects can 
be GCed whenever, but the class opcodes remain for the life of the script.  

> When a codeblock zval is executed, such as at $plusFive(4), the
> enclosing context can be passed to the anonymous function function to
> make the lexical $x statement bind to the proper variable.
>
> When the zval in $plusFive reaches zero references, it can
> subsequently free its references to the local variables of getAddr.
>
> As far as I can see, no herculean garbage collection or reference
> counting is required.

By separating the opcodes from the zval, you've side-stepped the need to do 
so.  That means declaring a dynamic function (or lambda, or whatever we end 
up calling this thing) inside a loop is no more expensive than instantiating 
a class inside of a loop.  I'm glad you know more about engine internals than 
I do. :-)

> Adding a new kind of zval?  Perhaps that's Herculean.  I don't know.

It sounds (to the layman) like it could/should be modeled on the way 
objects/classes are implemented in PHP 5, as "fancy references".  (I believe 
Sara Golemon had an article a while back that explained them that way.  Sara, 
my apologies if I am misquoting you.)  Whether that's a "new type of zval" or 
not I do not know.

> I hope I've gotten my zval terminology right.

Better than mine would be. :-)  And it seems to solve most of the 
implementation concerns that were raised earlier, at least at an abstract 
level.  The catch would be that I don't know the performance impact of making 
the compiler find and define these sorts of functions.  Can someone with more 
engine experience weigh in if the above is workable?

-- 
Larry Garfield			AIM: LOLG42
larry@garfieldtech.com		ICQ: 6817012

"If nature has made any one thing less susceptible than all others of 
exclusive property, it is the action of the thinking power called an idea, 
which an individual may exclusively possess as long as he keeps it to 
himself; but the moment it is divulged, it forces itself into the possession 
of every one, and the receiver cannot dispossess himself of it."  -- Thomas 
Jefferson

---

## #34108 — troels knak-nielsen — 2007-12-19T21:45:31+00:00

URL: https://externals.io/message/34040#34108 · In reply to: #34077

> Reading the prior discussion, I think either $_SCOPE['x'] or the
> lexical $x syntax is fine for accessing local variables in the
> enclosing scope.   But closures also should also support $this and
> static:: when the closure is defined in a method.

While I realise that the possibilities of static scope in PHP, are
intriguing, I really think it's a whole different discussion. Related,
yes. Interesting, yes. But let's keep it separate, to avoid derailing
the whole discussion.


> While we are at it, what's wrong with knowing the name? I can see why
> closure can be fun when you can dynamically use outer-scope variables.
> But when you can't, what exactly prevents one from just doing the function?

If the code block, which needs to create the function, could be run
more than once, it would try to define the function twice, leading to
a fatal error. Yes, I could guard it with is_function_defined(), but
that wouldn't be pretty. Technically speaking, you're right, but I
think aesthetics does have some weight. Or should. It's also my main
gripe against create_function() in the first place.


> First, about 100% on the first encounter for any user ever seeing
> closures in any other language. Second, all the confusion possible, like

So how big a part of PHP's userbase is that? I'm guessing, it's small.


> closures in any other language. Second, all the confusion possible, like
> "oh, closures! cool! let me do this and that! what, I can't use
> variables?! Are you kidding me?! WTF is it useful for?!"

True, but the people who will anguish over lack of closures, are
already tearing their hair out over create_function().


> I'm not sure I understand what you mean by "static scope" and "dynamic
> scope", ...

Static scope is a synonym for lexical scope.


> scope", but anyway thing that looks like closure but works like regular
> function definition that isn't - is not a very good idea, IMO.

Yes, that's the primary problem with the patch. I don't think it's as
big an issue, as you do and given the alternatives, I'm willing to
make that sacrifice. How's other peoples take on this point?

-- 
troels

---

## #34109 — Stanislav Malyshev — 2007-12-19T22:21:20+00:00

URL: https://externals.io/message/34040#34109 · In reply to: #34108

> So how big a part of PHP's userbase is that? I'm guessing, it's small.

If it's small, we don't need it in the language anyway.

> True, but the people who will anguish over lack of closures, are
> already tearing their hair out over create_function().

Having no function and knowing it is better than having something that 
looks like it but doesn't work. Saves time that would be spent 
unsuccessfully trying to make it work.
-- 
Stanislav Malyshev, Zend Software Architect
stas@zend.com   http://www.zend.com/
(408)253-8829   MSN: stas@zend.com

---

## #34110 — troels knak-nielsen — 2007-12-19T23:28:27+00:00

URL: https://externals.io/message/34040#34110 · In reply to: #34109

> > […]
>
> If it's small, we don't need it in the language anyway.

I think we need it. In the current incarnation, anonymous functions
are so impractical to use, that it's a barrier. I think that is
unfortunate, because it could be an interesting and useful direction
to take for PHP. The users, who don't know what a closure is, could
still learn to use anonymous functions and they would then not assume
closures. So even if the user base is small initially (naturally,
since the feature doesn't exist), it could grow. And for the rest of
us, it's at least better than create_function().

> Having no [closure] and knowing it is better than having something that
> looks like it but doesn't work. Saves time that would be spent
> unsuccessfully trying to make it work.
(I assume you meant closure?)

It can still work, without closures. Not all anonymous functions need
variable input and if they do, you can use currying. Admittedly,
currying has its own practical issues in PHP, but that's a separate
discussion. (And one, I'd like to take at a later time for that
matter)
The point is, that static lambda still has something to offer, even if
it doesn't allow closures.

-- 
troels

---

## #34112 — Stanislav Malyshev — 2007-12-19T23:44:53+00:00

URL: https://externals.io/message/34040#34112 · In reply to: #34110

> I think we need it. In the current incarnation, anonymous functions
> are so impractical to use, that it's a barrier. I think that is
> unfortunate, because it could be an interesting and useful direction
> to take for PHP. The users, who don't know what a closure is, could

Direction like what?
-- 
Stanislav Malyshev, Zend Software Architect
stas@zend.com   http://www.zend.com/
(408)253-8829   MSN: stas@zend.com

---

Next page: https://externals.io/message/34040.md?page=2
