# Closure local return

- Thread: https://externals.io/message/48174
- Original archive: https://news-web.php.net/php.internals/48174
- Started by mathieu.suen on 2010-05-03T13:01:19+00:00
- Messages: 10
- 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

---

## #48174 — mathieu.suen — 2010-05-03T13:01:19+00:00

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

Hi,

The statement 'return' in a closure is now returning from the scope that 
evaluate the closure (evaluation scope).
It could have been in an other way.
It could mean return from the  scope were the closure is create (define 
scope).

May be it could be interesting to have a syntax for returning from the 
define scope.
For example.

$findedElment = $myList->selectIfAbsent($fooo, function(){
     return 'No item founded'; //Retrun from the define scope
})
//Do somthing with $findedElment

So  I am asking if there has been some discussion on it or if it could 
be  added?

Thanks

--Mathieu Suen

---

## #48175 — Jille Timmermans — 2010-05-03T13:27:55+00:00

URL: https://externals.io/message/48174#48175 · In reply to: #48174

Are you serious?

Op 3-5-2010 15:01, mathieu.suen schreef:
> Hi,
>
> The statement 'return' in a closure is now returning from the scope that
> evaluate the closure (evaluation scope).
> It could have been in an other way.
> It could mean return from the scope were the closure is create (define
> scope).
>
> May be it could be interesting to have a syntax for returning from the
> define scope.
> For example.
>
> $findedElment = $myList->selectIfAbsent($fooo, function(){
> return 'No item founded'; //Retrun from the define scope
> })
> //Do somthing with $findedElment
>
> So I am asking if there has been some discussion on it or if it could be
> added?
>
> Thanks
>
> --Mathieu Suen

---

## #48176 — Etienne Kneuss — 2010-05-03T13:33:05+00:00

URL: https://externals.io/message/48174#48176 · In reply to: #48174

Hello,


> Hi,
>
> The statement 'return' in a closure is now returning from the scope that
> evaluate the closure (evaluation scope).
> It could have been in an other way.
> It could mean return from the  scope were the closure is create (define
> scope).
>
> May be it could be interesting to have a syntax for returning from the
> define scope.
> For example.
>
> $findedElment = $myList->selectIfAbsent($fooo, function(){
>    return 'No item founded'; //Retrun from the define scope
> })
> //Do somthing with $findedElment

uh? Could you please provide a clear example of what you mean, in
contrast of how it works now.

"return" returns to just after the call, returning somewhere else
doesn't make much sense IMHO.

>
> So  I am asking if there has been some discussion on it or if it could be
>  added?
>
> Thanks
>
> --Mathieu Suen
>
>
>
>



-- 
Etienne Kneuss
http://www.colder.ch

---

## #48178 — Christian Schneider — 2010-05-03T15:42:18+00:00

URL: https://externals.io/message/48174#48178 · In reply to: #48174

mathieu.suen wrote:
> May be it could be interesting to have a syntax for returning from the
> define scope.
> For example.
> 
> $findedElment = $myList->selectIfAbsent($fooo, function(){
>     return 'No item founded'; //Retrun from the define scope
> })
> //Do somthing with $findedElment

I think you actually misunderstand the difference in
http://en.wikipedia.org/wiki/Closure_%28computer_science%29#Differences_in_semantics

The way I read if the difference is wether it returns from the closure
function or the surrounding function *calling* it. Not the *defining* scope.

And no, it doesn't make sense in the PHP context IMHO.

- Chris

---

## #48180 — mathieu.suen — 2010-05-03T15:54:23+00:00

URL: https://externals.io/message/48174#48180 · In reply to: #48178

> I think you actually misunderstand the difference in
> http://en.wikipedia.org/wiki/Closure_%28computer_science%29#Differences_in_semantics
>
> The way I read if the difference is wether it returns from the closure
> function or the surrounding function *calling* it. Not the *defining* scope.
>
> And no, it doesn't make sense in the PHP context IMHO.
>
> - Chris
>
>    
"returns from the closure function"
To go were?

I had maintain a smalltalk compiler.
I did not misunderstand the difference.

The Common Lisp implementation is the more explicit one.
For exemple:

(defun eval-l1 (fct) (funcall fct))
(defun bar (x) (eval-l1 #'(lambda () (return-from bar 45)))
                         x)

(bar 23) -> 45

When funcall is apply you do not return from eval-l1. You return from bar.
Which is the defining scope of the lanbda.


-- Mathieu Suen

---

## #48181 — Etienne Kneuss — 2010-05-03T16:03:31+00:00

URL: https://externals.io/message/48174#48181 · In reply to: #48180

>
> > […]
>
> "returns from the closure function"
> To go were?
>
> I had maintain a smalltalk compiler.
> I did not misunderstand the difference.
>
> The Common Lisp implementation is the more explicit one.
> For exemple:
>
> (defun eval-l1 (fct) (funcall fct))
> (defun bar (x) (eval-l1 #'(lambda () (return-from bar 45)))
>                        x)
>
> (bar 23) -> 45
>
> When funcall is apply you do not return from eval-l1. You return from bar.
> Which is the defining scope of the lanbda.

So

<?php
$a = function() { return; }

$a();
echo "end";
?>

would it be an infinite loop? would it print end?

what about:

<?php
function foo($c) {
   bar($c);
   echo "f";
}

function bar($c) {
   $c();
   echo "b";
}
$c = function() { return; }

foo($c);

what would it print? "f"? "fb" ? nothing?

Please, if you want to propose design changes, make a decent proposal
first, add some examples, not simply "what if PHP did <vague>".

Best,

>
>
> -- Mathieu Suen
>
>
>



-- 
Etienne Kneuss
http://www.colder.ch

---

## #48182 — Tjerk Anne Meesters — 2010-05-03T21:34:59+00:00

URL: https://externals.io/message/48174#48182 · In reply to: #48178

On Mon, May 3, 2010 at 11:42 PM, Christian Schneider
<cschneid@cschneid.com> wrote:
> mathieu.suen wrote:
> > […]
>
> I think you actually misunderstand the difference in
> http://en.wikipedia.org/wiki/Closure_%28computer_science%29#Differences_in_semantics
>
> The way I read if the difference is wether it returns from the closure
> function or the surrounding function *calling* it. Not the *defining* scope.

Mathieu is right in that it would return from the defining scope, but
it's important to note that when the closure is called the function
that defined the closure is still on the stack.

Consider the following example:

function f() { return function() { return; } }
$c = f();
$c();

If the semantics of Smalltalk were to apply, this would give an error,
because it's impossible to return from f() when the closure is being
called.

>
> And no, it doesn't make sense in the PHP context IMHO.

I second that ;-) In terms of clarity I find that the ECMAScript
implementation is much clearer.

>
> - Chris
>
> --
> 
> 
>
>



-- 
--
Tjerk

---

## #48185 — mathieu.suen — 2010-05-04T07:08:25+00:00

URL: https://externals.io/message/48174#48185 · In reply to: #48181

> 
>    
> > […]
> So
>
> <?php
> $a = function() { return; }
>
> $a();
> echo "end";
> ?>
>
> would it be an infinite loop? would it print end?
>
> what about:
>
> <?php
> function foo($c) {
>     bar($c);
>     echo "f";
> }
>
> function bar($c) {
>     $c();
>     echo "b";
> }
> $c = function() { return; }
>
> foo($c);
>
> what would it print? "f"? "fb" ? nothing?
>    

This is an interesting question.
It depend on whether you consider your lanbda as a continuation.
The other safe possibility is to generate an error when you evaluate a 
lambda
  were the context has escape which is the case in your example.

But a would not change the semantic of "return" just chose an other keyword.

$c = function() { return-from-def-scope; }


> Please, if you want to propose design changes, make a decent proposal
> first, add some examples, not simply "what if PHP did<vague>".
>    

No big deal, I don't want to make any proposition,  I am just asking.
I am not using PHP as a language for fun.

> Best,
>    
> > […]

-- Mathieu Suen

---

## #48186 — mathieu.suen — 2010-05-04T07:24:20+00:00

URL: https://externals.io/message/48174#48186 · In reply to: #48182

> On Mon, May 3, 2010 at 11:42 PM, Christian Schneider
> <cschneid@cschneid.com>  wrote:
>    
> > […]
> Mathieu is right in that it would return from the defining scope, but
> it's important to note that when the closure is called the function
> that defined the closure is still on the stack.
>
> Consider the following example:
>
> function f() { return function() { return; } }
> $c = f();
> $c();
>
> If the semantics of Smalltalk were to apply, this would give an error,
> because it's impossible to return from f() when the closure is being
> called.
>
>    
> > […]
> I second that ;-) In terms of clarity I find that the ECMAScript
> implementation is much clearer.
>
>    

Of course ECMAScript has the same semantic than PHP. :)
(for this particular point, I don't mean for the rest)
May be one could fink of a different syntax:

function() { return-from-def-scope; }

And that would also remove the ambiguity in your example:

function f() { return function() { return-from-def-scope; } }




> > […]
>
>
>    

-- Mathieu Suen

---

## #48303 — mathieu.suen — 2010-05-17T09:48:43+00:00

URL: https://externals.io/message/48174#48303 · In reply to: #48178

> mathieu.suen wrote:
>    
> > […]
> I think you actually misunderstand the difference in
> http://en.wikipedia.org/wiki/Closure_%28computer_science%29#Differences_in_semantics
>
> The way I read if the difference is wether it returns from the closure
> function or the surrounding function *calling* it. Not the *defining* scope.
>
>    
> And no, it doesn't make sense in the PHP context IMHO.
>    
You need to give me more argument on that point.
> - Chris
>
>    

-- Mathieu Suen
