> Is it `returnType as $thing` ? That doesn't read well, but I don't see us making return type movable. as/use should be placable in either order, but maybe we give them required order anyway?
I don't know if that would be a problem, because today we can have it "function(): Type use($x)", so "Type use($x)"? I have to agree that it may sound strange at first, but I also can't imagine many ways of doing things differently without creating more complexities. (1) function as $lambda (... $args): returnType use ($captures...) {} (2) function (... $args) as $lambda: returnType use ($captures...) {} (3) function (... $args): returnType as $lambda use ($captures...) {} // original suggestion (4) function (... $args): returnType use ($captures...) as $lambda {} (4.b) function (... $args): returnType as $lambda {} // without use() will be same as (3) Or, define a "inline function" with its own name, visible only within the scope itself. Similar to how JS works. (5) function $lambda(... $args): returnType use ($captures...) {} To be honest, as I typed what came to mind, I ended up preferring this last option. JS way: function fb(n) { if (n === 0) return 0; if (n === 1) return 1; return fb(n - 1) + fb(n - 2); } PHP (self-referenced) way: function $fb($n): int { if ($n === 0) return 0; if ($n === 1) return 1; return $fb($n - 1) + $fb($n - 2); } Currently PHP will accepts the same JS solution, but it will exposes fb() to global scope and could causes errors: function test() { function fb($n): int { if ($n === 0) return 0; if ($n === 1) return 1; return fb($n - 1) + fb($n - 2); } var_dump(fb(10)); } test(); test(); // Fatal error: Cannot redeclare fb() Atenciosamente, David Rodrigues Em sex., 20 de nov. de 2020 às 12:14, Sara Golemon <poll...@php.net> escreveu: > On Wed, Nov 11, 2020 at 12:37 PM David Rodrigues <david.pro...@gmail.com> > wrote: > >> My suggestion is to reuse the keyword `as` to set a variable that will >> represent its own closure. It is more flexible once that we could choose >> any name and reuse in nested closures. It is pretty similar to how SQL >> works too. >> >> function fn1() as $lambda1 { >> return function() as $lambda2 use ($lambda1) { >> return [ gettype($lambda1), gettype($lambda2) ]; >> }; >> } >> >> > My initial reaction to this is: Technically doable (easy even), but looks > a bit... ugly? weird? surprising? > > I think my main objection to it is how much is now stacked after the > parameter list: > > function($args...) : returnType as $local use ($captures...) { > ... > }; > > That's a lot of... stuff (granted, we have most of it already) and how it > looks when stacked together gets funky. > > Is it `returnType as $thing` ? That doesn't read well, but I don't see us > making return type movable. as/use should be placable in either order, but > maybe we give them required order anyway? > > TLDR; Long winded way to say I'm not inherently against this, and I like > the idea of a lambda being able to recurse (not that I can ever recall > having use for it, but maybe I fell back on named functions for that), I > just think it's getting a bit wordy. > > -Sara >