On Fri, Nov 20, 2020, at 10:01 AM, David Rodrigues wrote:
> > 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.

I kind of like that option, too.  The main question in my mind is whether that 
means the same variable is used internally and externally or not.  That is, my 
knee-jerk reaction when reading that was to ask if this would be confusing:

$fib = function $f(int $n): int { ... }

Viz, do $fib and $f need to match?

It looks from the example below like you're saying it's actually:

function $fib(int $n): int { ... }

Where $fib is then both the internal and external variable name.

That seems unexpected.  It may be the best way to go about it, but it feels 
surprising as a user space developer.

I do think something like that is probably the best way forward, though.

The other question of course is short-lambdas.  Would that mean this?

fn $fib(int $n): int => match($n) {
  0 => 0,
  1 => 1,
  default => $fib(n - 1) + $fib($n - 2),
};

If the function is inlined, does that mean the variable name leaks and thus 
remains in scope (and the closure thus using memory)?

array_map(fn $fib(int $n): int => match($n) {
  0 => 0,
  1 => 1,
  default => $fib(n - 1) + $fib($n - 2),
}, range(1, 6));

print $fib(5); // Does this work?

(No you shouldn't write it like that, but since Fibonacci is the example we're 
working with...)

--Larry Garfield

-- 
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: https://www.php.net/unsub.php

Reply via email to