# [RFC]

- Thread: https://externals.io/message/108459
- Original archive: https://news-web.php.net/php.internals/108459
- Started by Manuel Canga on 2020-02-11T11:13:54+00:00
- Messages: 65
- Page 1 of 3: messages 1 to 25 (25 per page, add `?page=N` to the URL)
- Next page: https://externals.io/message/108459.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

---

## #108459 — Manuel Canga — 2020-02-11T11:13:54+00:00

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

Hi internals,
I Would like to present a possible new "::func resolution" for your
consideration.

In first place, PHP now support "::class" in this way:

use My\I18N;

$mapped_array = array_map([I18N::class, 'translate'], $array);
It avoid add Full I18N  namespace in callback.

However with functions is different:

use function \My\I18N\i18n_translate;

$mapped_array = array_map('\My\I18N\i18n_translate', $array);

What is the useful here of importing the function?.
My proposal is ":func" in order to avoid full namespace in callback of
functions. E.g:

use function \My\I18N\i18n_translate;

$mapped_array = array_map(i18n_translate::func, $array);

"<string>::func" should validate if a function with `<string>` is imported.
In this case, "<string>::func" is replaced with FQN of this function,
otherwise with only "<string>"

What is your opinion ? Do you see it useful ?

Thanks and I'm sorry for my English( I'm a Spanish ).

Regards

---

## #108461 — Manuel Canga — 2020-02-11T11:26:45+00:00

URL: https://externals.io/message/108459#108461 · In reply to: #108459

I'm sorry. I forgot subject due to nerves. I add it.


>
> Hi internals,
> I Would like to present a possible new "::func resolution" for your consideration.
>
> In first place, PHP now support "::class" in this way:
>
> use My\I18N;
>
> $mapped_array = array_map([I18N::class, 'translate'], $array);
> It avoid add Full I18N  namespace in callback.
>
> However with functions is different:
>
> use function \My\I18N\i18n_translate;
>
> $mapped_array = array_map('\My\I18N\i18n_translate', $array);
>
> What is the useful here of importing the function?.
> My proposal is ":func" in order to avoid full namespace in callback of functions. E.g:
>
> use function \My\I18N\i18n_translate;
>
> $mapped_array = array_map(i18n_translate::func, $array);
>
> "<string>::func" should validate if a function with `<string>` is imported. In this case, "<string>::func" is replaced with FQN of this function, otherwise with only "<string>"
>
> What is your opinion ? Do you see it useful ?
>
> Thanks and I'm sorry for my English( I'm a Spanish ).
>
> Regards

---

## #108462 — Tom Gerrits — 2020-02-11T11:33:34+00:00

URL: https://externals.io/message/108459#108462 · In reply to: #108459

Hi Manuel

Thanks for bringing this idea forward.

Adding a non-string construct to reference functions uniquely seems 
like a good idea and would be consistent with the existing ::class 
keyword for classes.

I'm wondering if it would be useful to take a page out of the book of 
other programming languages in order to shorten the notation further:

> // First file.
> namespace A\B;
> 
> function foo() { }
> 
> // Second file.
> use function A\B\foo;
> 
> function bar(callable $c) { }
> 
> 
> // Current approach.
> bar('\A\B\foo');
> 
> // Notation without parantheses, references imported function.
> // Similarly, in the future, Class::method could reference a
> // class method.
> bar(foo);
> 
> // Notation with references, could be expanded in the future
> // for static class methods, such as &Class::method.
> bar(&foo);

The second example can already be achieved by defining your own 
constant that references the fully qualified name and importing it. 
However, an explicit, separate, import of these constants is still 
needed, which is not ideal.

A suffix "::func" would also be useful, but allowing shorter notation 
would improve readability when using the functional programming 
paradigm and composing functions.

Op di, feb 11, 2020 at 12:13 schreef Manuel Canga 
<manuelcanga@gmail.com>:
> Hi internals,
> I Would like to present a possible new "::func resolution" for your
> consideration.
> 
> In first place, PHP now support "::class" in this way:
> 
> use My\I18N;
> 
> $mapped_array = array_map([I18N::class, 'translate'], $array);
> It avoid add Full I18N  namespace in callback.
> 
> However with functions is different:
> 
> use function \My\I18N\i18n_translate;
> 
> $mapped_array = array_map('\My\I18N\i18n_translate', $array);
> 
> What is the useful here of importing the function?.
> My proposal is ":func" in order to avoid full namespace in callback of
> functions. E.g:
> 
> use function \My\I18N\i18n_translate;
> 
> $mapped_array = array_map(i18n_translate::func, $array);
> 
> "<string>::func" should validate if a function with `<string>` is 
> imported.
> In this case, "<string>::func" is replaced with FQN of this function,
> otherwise with only "<string>"
> 
> What is your opinion ? Do you see it useful ?
> 
> Thanks and I'm sorry for my English( I'm a Spanish ).
> 
> Regards

---

## #108463 — Manuel Canga — 2020-02-11T11:47:05+00:00

URL: https://externals.io/message/108459#108463 · In reply to: #108462

Hi Tom,

I thought about that short version. However, in order to avoid
conflicts with constants, I chosen "::func". If this is not problem in
low level of PHP, then I agree with you in shorter version is better.

Thanks for your opinion, Tom






>
> Hi Manuel
>
> Thanks for bringing this idea forward.
>
> Adding a non-string construct to reference functions uniquely seems
> like a good idea and would be consistent with the existing ::class
> keyword for classes.
>
> I'm wondering if it would be useful to take a page out of the book of
> other programming languages in order to shorten the notation further:
>
> > […]
>
> The second example can already be achieved by defining your own
> constant that references the fully qualified name and importing it.
> However, an explicit, separate, import of these constants is still
> needed, which is not ideal.
>
> A suffix "::func" would also be useful, but allowing shorter notation
> would improve readability when using the functional programming
> paradigm and composing functions.
>
> Op di, feb 11, 2020 at 12:13 schreef Manuel Canga
> <manuelcanga@gmail.com>:
> > […]

---

## #108464 — Diogo Galvao — 2020-02-11T11:51:39+00:00

URL: https://externals.io/message/108459#108464 · In reply to: #108459

>
> Hi internals,
> I Would like to present a possible new "::func resolution" for your
> consideration.
...
> use function \My\I18N\i18n_translate;
>
> $mapped_array = array_map(i18n_translate::func, $array);
...
> What is your opinion ? Do you see it useful ?

I've wished for this on many occasions and think it'd be really useful, as long
as it could work with methods as well:

$mapped_array = array_map(I18N::translate::function, $array);

For what it's worth I guess it could just return [I18N::class, 'translate'].

Also for keeping consistency and avoiding new keywords wouldn't it fit better as
::function instead of ::func?


Best regards.
Diogo

---

## #108466 — Manuel Canga — 2020-02-11T12:08:55+00:00

URL: https://externals.io/message/108459#108466 · In reply to: #108464

Hi, Diogo

That's right. I could be useful with classes as well.

I was thinking about 'function' or 'func'. 'function' is more
semantic, but 'func' is used with "short functions"( PHP 7.4 ) and
these will be used for callbacks. Then I think 'func' will be more
consistent.

Thanks, Diogo,


>
> 
> > […]
> ...
> > […]
> ...
> > […]
>
> I've wished for this on many occasions and think it'd be really useful, as long
> as it could work with methods as well:
>
> $mapped_array = array_map(I18N::translate::function, $array);
>
> For what it's worth I guess it could just return [I18N::class, 'translate'].
>
> Also for keeping consistency and avoiding new keywords wouldn't it fit better as
> ::function instead of ::func?
>
>
> Best regards.
> Diogo

---

## #108469 — Nicolas Grekas — 2020-02-11T12:16:05+00:00

URL: https://externals.io/message/108459#108469 · In reply to: #108464

Le mar. 11 févr. 2020 à 12:52, Diogo Galvao <diogo86@gmail.com> a écrit :

> On Tue, Feb 11, 2020 at 8:14 AM Manuel Canga <manuelcanga@gmail.com>
> wrote:
> > […]
> ...
> > […]
> ...
> > […]
>
> I've wished for this on many occasions and think it'd be really useful, as
> long
> as it could work with methods as well:
>
> $mapped_array = array_map(I18N::translate::function, $array);
>
> For what it's worth I guess it could just return [I18N::class,
> 'translate'].
>

I wish this would return a Closure instead, making $foo::function the
equivalent of Closure::fromCallable($foo).

Nicolas

---

## #108472 — Manuel Canga — 2020-02-11T12:42:52+00:00

URL: https://externals.io/message/108459#108472 · In reply to: #108469

On Tue, 11 Feb 2020 at 13:16, Nicolas Grekas
<nicolas.grekas+php@gmail.com> wrote:
>
>
>
> Le mar. 11 févr. 2020 à 12:52, Diogo Galvao <diogo86@gmail.com> a écrit :
> > […]
>
>
> I wish this would return a Closure instead, making $foo::function the equivalent of Closure::fromCallable($foo).

Hi, Nicolas,

Currently, when <class_name>::class is used, class with <class_name>
can or cannot exists in that moment.
Using <function_name>::func( or <function_name>::function ), I think
should keep the same behavior.

Using ::func as alias of "Closure::fromCallable" check if function
exists in that moment. It is certainly useful, but, I think it's more
important be consistent

Thanks, Nicolas,

---

## #108473 — Guilliam Xavier — 2020-02-11T12:47:30+00:00

URL: https://externals.io/message/108459#108473 · In reply to: #108466

Hi,


>
> I was thinking about 'function' or 'func'. 'function' is more
> semantic, but 'func' is used with "short functions"( PHP 7.4 ) and
> these will be used for callbacks. Then I think 'func' will be more
> consistent.

Actually PHP 7.4's short closures / array functions use `fn`, not `func`.
But `::fn` would look... weird.
I agree with Diogo that `::function` would be more consistent.

-- 
Guilliam Xavier

---

## #108474 — Manuel Canga — 2020-02-11T12:57:04+00:00

URL: https://externals.io/message/108459#108474 · In reply to: #108473

Ups!. What big mistake!

Then ::function should be a better option.

Thanks, Guilliam,


>
> Hi,
>
> 
> > […]
>
> Actually PHP 7.4's short closures / array functions use `fn`, not `func`.
> But `::fn` would look... weird.
> I agree with Diogo that `::function` would be more consistent.
>
> --
> Guilliam Xavier

---

## #108480 — Nikita Popov — 2020-02-11T13:19:19+00:00

URL: https://externals.io/message/108459#108480 · In reply to: #108472

> On Tue, 11 Feb 2020 at 13:16, Nicolas Grekas
> <nicolas.grekas+php@gmail.com> wrote:
> > […]
> :
> > […]
> wrote:
> > […]
> as long
> > […]
> 'translate'].
> > […]
> equivalent of Closure::fromCallable($foo).
>
> Hi, Nicolas,
>
> Currently, when <class_name>::class is used, class with <class_name>
> can or cannot exists in that moment.
> Using <function_name>::func( or <function_name>::function ), I think
> should keep the same behavior.
>
> Using ::func as alias of "Closure::fromCallable" check if function
> exists in that moment. It is certainly useful, but, I think it's more
> important be consistent
>

Checking whether the function exists is really unavoidable for functions,
because you don't know whether

namespace Foo;
var_dump(strlen::function);

refers to 'Foo\strlen' or 'strlen', without first trying to look up the
former.

I agree with Nicolas that this kind of feature would provide the most value
if it created a Closure. This would circumvent all the issues outlined in
https://wiki.php.net/rfc/consistent_callables.

Regards,
Nikita

---

## #108483 — Manuel Canga — 2020-02-11T14:48:18+00:00

URL: https://externals.io/message/108459#108483 · In reply to: #108480

>
> 
> > […]
>
>
> Checking whether the function exists is really unavoidable for functions, because you don't know whether
>
> namespace Foo;
> var_dump(strlen::function);
>
> refers to 'Foo\strlen' or 'strlen', without first trying to look up the former.
>
> I agree with Nicolas that this kind of feature would provide the most value if it created a Closure. This would circumvent all the issues outlined in https://wiki.php.net/rfc/consistent_callables.
>
> Regards,
> Nikita

Thanks, Nikita. I don't know that RFC.

In that case, why don't use ::callable ?. This will be useful for
functions and others callable. E.g:

enqueue_task([I18n::class, 'translate']::callable);
enqueue_task(translate::callable);

Would it be hard to implement in PHP ?

---

## #108484 — Chase Peeler — 2020-02-11T14:51:15+00:00

URL: https://externals.io/message/108459#108484 · In reply to: #108480

> On Tue, Feb 11, 2020 at 1:43 PM Manuel Canga <manuelcanga@gmail.com>
> wrote:
>
> > […]
> écrit
> > […]
> useful,
> > […]
>
> Checking whether the function exists is really unavoidable for functions,
> because you don't know whether
>
> namespace Foo;
> var_dump(strlen::function);
>
> refers to 'Foo\strlen' or 'strlen', without first trying to look up the
> former.
>
> I agree with Nicolas that this kind of feature would provide the most value
> if it created a Closure. This would circumvent all the issues outlined in
> https://wiki.php.net/rfc/consistent_callables.
>
> Regards,
> Nikita
>

Can anyone thing of a use-case where you would want a string name of a
function and a callable would not be acceptable, besides possibly debugging
code that said 'echo "I'm calling ".myfunction::function;'? Everything that
I can think of that accepts a function name, also accepts a callable (e.g.
array_map), but I could be forgetting something.

If not, then I think it makes sense to return a callable. It might not be
entirely consistent with the behavior of ::class, but, a class isn't
entirely consistent with a method/function either, so I think there is some
latitude for small differences.

As for the ::func vs ::function. I think ::function is safer, since it's a
reserved word. Otherwise you might run into issues with something like this:

class foo {
  const func = "bar";
}

function foo(){}

echo foo::func;

Probably not something that happens very often, but, I think the 4 extra
characters to prevent it would be worth it.

-- 
Chase Peeler
chasepeeler@gmail.com

---

## #108485 — Manuel Canga — 2020-02-11T15:13:34+00:00

URL: https://externals.io/message/108459#108485 · In reply to: #108484

>
>
>
> 
> > […]
>
>
> Can anyone thing of a use-case where you would want a string name of a function and a callable would not be acceptable, besides possibly debugging code that said 'echo "I'm calling ".myfunction::function;'? Everything that I can think of that accepts a function name, also accepts a callable (e.g. array_map), but I could be forgetting something.
>
> If not, then I think it makes sense to return a callable. It might not be entirely consistent with the behavior of ::class, but, a class isn't entirely consistent with a method/function either, so I think there is some latitude for small differences.
>
> As for the ::func vs ::function. I think ::function is safer, since it's a reserved word. Otherwise you might run into issues with something like this:
>
> class foo {
>   const func = "bar";
> }
>
> function foo(){}
>
> echo foo::func;
>
> Probably not something that happens very often, but, I think the 4 extra characters to prevent it would be worth it.
>
> --
> Chase Peeler
> chasepeeler@gmail.com

Hi, Chase,

Yes, '::function' is better. I don't know because I thought 'func' was
used with short functions.
One case which string can be useful but a callable would not be acceptable is:

array_map([I18N::class, translate::function] );

Although it's true that can have side effects.

Thanks, Chase,

---

## #108486 — Levi Morrison via internals — 2020-02-11T15:27:47+00:00

URL: https://externals.io/message/108459#108486 · In reply to: #108459

I have three immediate thoughts:

1. It should be `fn` or `function`; reserving a new word even if it's
contextual is pointless here.
2. It should support methods.
3. It should return a closure, not a string. The reason is for
consistency with methods, where we want to capture the scope at the
time the closure is created, not ran. Imagine passing a private method
as a callback; if it was done via `[$obj, 'privatemethod']` then it
will fail at runtime, because the scope it's called in doesn't have
access.

---

## #108487 — Manuel Canga — 2020-02-11T16:39:00+00:00

URL: https://externals.io/message/108459#108487 · In reply to: #108486

On Tue, 11 Feb 2020 at 16:27, Levi Morrison <levi.morrison@datadoghq.com>
wrote:
>
> I have three immediate thoughts:
>
> 1. It should be `fn` or `function`; reserving a new word even if it's
> contextual is pointless here.
> 2. It should support methods.
> 3. It should return a closure, not a string. The reason is for
> consistency with methods, where we want to capture the scope at the
> time the closure is created, not ran. Imagine passing a private method
> as a callback; if it was done via `[$obj, 'privatemethod']` then it
> will fail at runtime, because the scope it's called in doesn't have
> access.

Hi, Levi,

1. Yes
2. Yes, I agree
3. Wouldn't it be the opposite?. E.g:

class Foo {
    static function run_callable(callable $callable) {
        $callable();
    }

    private static function private_function() {
        echo "bar";
    }
}

Foo::run_callable( [ 'Foo', 'private_function' ] );


Currently, this is valid. However, the following is fatal:

class Foo {
    static function run_callable(callable $callable) {
        $callable();
    }

    private static function private_function() {
        echo "bar";
    }
}

Foo::run_callable(Closure::fromCallable(['Foo', 'private_function']));

The same with functions. If "::function" would retrieve a closure,
some code could breaking
when people switch string function syntax( e.g: 'strlen' )  to
"::function" syntax(  strlen::function ).

On other hand, other option is ::callable, This is different to
::function, because it would affect to other
callables, so that, returns could be other( Closure )

---

## #108488 — Dan Ackroyd — 2020-02-11T16:48:53+00:00

URL: https://externals.io/message/108459#108488 · In reply to: #108469

Nicolas Grekas wrote:
> I wish this would return a Closure instead, making $foo::function the
> equivalent of Closure::fromCallable($foo).

I didn't include the following in that RFC, because I thought it would
be too controversial, but I think it's worth considering a new syntax
for this.

Given the code:

function foo();
class Zoq {
    public function Fot() {}
    public static function Pik() {}
}
$obj = new Zoq();


Then these:

$(foo);
$($obj, Fot);
$(Zoq, Fot);

Would be equivalent to:

Closure::fromCallable('foo');
Closure::fromCallable([$obj, 'Fot']);
Closure::fromCallable('Zoq::Fot'); or Closure::fromCallable(['Zoq', 'Fot']);

Or similar.

The justification for having a dedicated syntax is that I think
readability is quite important in code, and it's reasonably common for
me to have quite long lists of 'callables'.

[Bar::class, foo1::function],
[Bar::class, foo2::function],
[Bar::class, foo3::function],
[Bar::class, foo4::function]

vs

$(Bar, foo1),
$(Bar, foo2),
$(Bar, foo3),
$(Bar, foo4)

The latter is far easier to read for me.

Nikita Popov wrote:
> This would circumvent all the issues outlined in
> https://wiki.php.net/rfc/consistent_callables.

Probably there would still be some issues with some of the weird stuff
happening internally in SPL related code where the deep horrors
rest...but we can leave them alone...and they might not wake.

cheers
Dan
Ack

---

## #108490 — Manuel Canga — 2020-02-11T17:21:01+00:00

URL: https://externals.io/message/108459#108490 · In reply to: #108488

>
> Nicolas Grekas wrote:
> > […]
>
> I didn't include the following in that RFC, because I thought it would
> be too controversial, but I think it's worth considering a new syntax
> for this.
>
> Given the code:
>
> function foo();
> class Zoq {
>     public function Fot() {}
>     public static function Pik() {}
> }
> $obj = new Zoq();
>
>
> Then these:
>
> $(foo);
> $($obj, Fot);
> $(Zoq, Fot);
>
> Would be equivalent to:
>
> Closure::fromCallable('foo');
> Closure::fromCallable([$obj, 'Fot']);
> Closure::fromCallable('Zoq::Fot'); or Closure::fromCallable(['Zoq', 'Fot']);
>
> Or similar.
>
> The justification for having a dedicated syntax is that I think
> readability is quite important in code, and it's reasonably common for
> me to have quite long lists of 'callables'.
>
> [Bar::class, foo1::function],
> [Bar::class, foo2::function],
> [Bar::class, foo3::function],
> [Bar::class, foo4::function]
>
> vs
>
> $(Bar, foo1),
> $(Bar, foo2),
> $(Bar, foo3),
> $(Bar, foo4)
>
> The latter is far easier to read for me.
>
> Nikita Popov wrote:
> > […]
>
> Probably there would still be some issues with some of the weird stuff
> happening internally in SPL related code where the deep horrors
> rest...but we can leave them alone...and they might not wake.
>
> cheers
> Dan
> Ack

Hi, Dan,

I like it, although syntax reminds me to JQuery syntax.

Thanks

---

## #108492 — Manuel Canga — 2020-02-11T17:41:45+00:00

URL: https://externals.io/message/108459#108492 · In reply to: #108488

>
> Nicolas Grekas wrote:
> > […]
>
> I didn't include the following in that RFC, because I thought it would
> be too controversial, but I think it's worth considering a new syntax
> for this.
>
> Given the code:
>
> function foo();
> class Zoq {
>     public function Fot() {}
>     public static function Pik() {}
> }
> $obj = new Zoq();
>
>
> Then these:
>
> $(foo);
> $($obj, Fot);
> $(Zoq, Fot);
>
> Would be equivalent to:
>
> Closure::fromCallable('foo');
> Closure::fromCallable([$obj, 'Fot']);
> Closure::fromCallable('Zoq::Fot'); or Closure::fromCallable(['Zoq', 'Fot']);
>
> Or similar.
>
> The justification for having a dedicated syntax is that I think
> readability is quite important in code, and it's reasonably common for
> me to have quite long lists of 'callables'.
>
> [Bar::class, foo1::function],
> [Bar::class, foo2::function],
> [Bar::class, foo3::function],
> [Bar::class, foo4::function]
>
> vs
>
> $(Bar, foo1),
> $(Bar, foo2),
> $(Bar, foo3),
> $(Bar, foo4)
>
> The latter is far easier to read for me.
>
> Nikita Popov wrote:
> > […]
>
> Probably there would still be some issues with some of the weird stuff
> happening internally in SPL related code where the deep horrors
> rest...but we can leave them alone...and they might not wake.
>
> cheers
> Dan
> Ack

Other option:

Closure::fromCallable('foo');
Closure::fromCallable([$obj, 'Fot']);
Closure::fromCallable('Zoq::Fot'); or Closure::fromCallable(['Zoq', 'Fot']);

to

<Foo>
<$foo, Fot>
<Zoq::Fot>
<Zoq,'Fot>

E.g:

array_map(<strlen>, $array);
array_map(<I18n, Translate>, $array);

Do you like ?

---

## #108494 — Larry Garfield — 2020-02-11T18:46:12+00:00

URL: https://externals.io/message/108459#108494 · In reply to: #108486

> I have three immediate thoughts:
> 
> 1. It should be `fn` or `function`; reserving a new word even if it's
> contextual is pointless here.
> 2. It should support methods.
> 3. It should return a closure, not a string. The reason is for
> consistency with methods, where we want to capture the scope at the
> time the closure is created, not ran. Imagine passing a private method
> as a callback; if it was done via `[$obj, 'privatemethod']` then it
> will fail at runtime, because the scope it's called in doesn't have
> access.

I would love a nicer way to reference function names; it's really ugly to do functional code in PHP otherwise, or even just dynamic function logic within a namespace.  If I never have to write $fn = __NAMESPACE__ . '\a_func' again, it will be too soon. :-)

I agree with Levi on points 2 and 3, for reasons already stated.

For bikeshedding on the syntax to use, I defer to the engine authors who can say what is possible to implement.  However, the guideline should be *short*.  If we make referencing a function too verbose, no one will want to use it and readability will be harmed.  Double-whammy.  That's why I'd say adding ten characters (::function) is a definite no-no.

::fn would work.  There was previous discussion of ::name, which could then also apply to classes and objects.  That would be acceptable, I think.  

Other alternatives like &(funName) et al are also fine if they actually work; Again, I defer to the implementers on that.

But in general, yes please!

--Larry Garfield

---

## #108495 — michal.brzuchalski@gmail.com — 2020-02-11T19:01:57+00:00

URL: https://externals.io/message/108459#108495 · In reply to: #108492

wt., 11 lut 2020, 18:42 użytkownik Manuel Canga <manuelcanga@gmail.com>
napisał:

> 
> > […]
> 'Fot']);
> > […]
>
> Other option:
>
> Closure::fromCallable('foo');
> Closure::fromCallable([$obj, 'Fot']);
> Closure::fromCallable('Zoq::Fot'); or Closure::fromCallable(['Zoq',
> 'Fot']);
>
> to
>
> <Foo>
> <$foo, Fot>
> <Zoq::Fot>
> <Zoq,'Fot>
>
> E.g:
>
> array_map(<strlen>, $array);
> array_map(<I18n, Translate>, $array);
>
> Do you like ?
>

That reminds me an old draft RFC https://wiki.php.net/rfc/short-closures
where I thought curly braces can be used to create closure from syntax
nearly the same as invoking but without parentheses.

That way we can provide clear intent - I mean if whatever is around: curly
braces or $ with parentheses IMHO it should be consistent with call like
format. For example:

$(strlen) or {strlen} for Closure::fromCallable('foo')

$($foo->Fot) or {$foo->Fot} for Closure::fromCallable([$obj, 'Fot'])

$(Zoq::Fot) or {Zoq::Fot} for Closure::fromCallable('Zoq::Fot')

Etc. The same inside like a call but wrapped in something and with no
parentheses part.

Cheers,
--
Michał Brzuchalski

---

## #108496 — Dik Takken — 2020-02-11T19:08:28+00:00

URL: https://externals.io/message/108459#108496 · In reply to: #108488

> I didn't include the following in that RFC, because I thought it would
> be too controversial, but I think it's worth considering a new syntax
> for this.
> 
> Given the code:
> 
> function foo();
> class Zoq {
>     public function Fot() {}
>     public static function Pik() {}
> }
> $obj = new Zoq();
> 
> 
> Then these:
> 
> $(foo);
> $($obj, Fot);
> $(Zoq, Fot);
> 
> Would be equivalent to:
> 
> Closure::fromCallable('foo');
> Closure::fromCallable([$obj, 'Fot']);
> Closure::fromCallable('Zoq::Fot'); or Closure::fromCallable(['Zoq', 'Fot']);
> 
> Or similar.

Given the fact that $() would only accept functions and methods as
argument, this idea could be taken one step further by writing:

    $(foo);
    $($obj->Fot);
    $(Zoq::Fot);

Referring to a method like this is normally not possible because it is
ambiguous. However, wrapped inside $(), which only accepts functions and
methods, the ambiguity disappears.

The $() syntax is nice and short. And something completely new. As new
syntax can only be 'spent' once, more familiar alternatives should be
explored as well. Thinking about the existing list() and array() syntax,
another possibility could be:

    closure(foo);
    closure($obj->Fot);
    closure(Zoq::Fot);

It is slightly longer but more familiar syntax.


>
> I would love a nicer way to reference function names; it's really ugly
to do functional code in PHP otherwise, or even just dynamic function
logic within a namespace.  If I never have to write $fn = __NAMESPACE__
. '\a_func' again, it will be too soon. :-)

Perhaps Larry can convince us all to go for something like $() by
posting a fabulous functional programming example?

Regards,
Dik Takken

---

## #108497 — Dan Ackroyd — 2020-02-11T19:20:50+00:00

URL: https://externals.io/message/108459#108497 · In reply to: #108496

> $($obj->Fot);
> However, wrapped inside $(), which only accepts
> functions and methods

I don't think I meant that. I think it still should accept variables
as well, so it would still be ambiguous as to whether it referred to
the value of the property Fot of object $obj, or the method Fot on the
$obj class.

Also, just in general using something that has the same syntax as
current PHP code, but a different meaning, sounds like a bad idea.

> another possibility could be: closure(foo);

That has the downside of exposing the implementation detail, that it's
implemented through closures, which would be a regrettable choice if
we ever decided to change the implementation details in the future.

And it also has the downside of looking like a function call, but
having different rules for what can be put inside it:

function foo();
closure(foo); // fine.
special_closure(foo); // syntax error, undefined constant foo.

 Although avoiding new syntax is good in general, if what can go
inside it is different, then I think it requires a new syntax.

cheers
Dan
Ack

---

## #108498 — michal.brzuchalski@gmail.com — 2020-02-11T19:22:41+00:00

URL: https://externals.io/message/108459#108498 · In reply to: #108496

wt., 11 lut 2020, 20:08 użytkownik Dik Takken <d.h.j.takken@freedom.nl>
napisał:

> 
> > […]
> 'Fot']);
> > […]
>
> Given the fact that $() would only accept functions and methods as
> argument, this idea could be taken one step further by writing:
>
>     $(foo);
>     $($obj->Fot);
>     $(Zoq::Fot);
>
> Referring to a method like this is normally not possible because it is
> ambiguous. However, wrapped inside $(), which only accepts functions and
> methods, the ambiguity disappears.
>
> The $() syntax is nice and short. And something completely new. As new
> syntax can only be 'spent' once, more familiar alternatives should be
> explored as well. Thinking about the existing list() and array() syntax,
> another possibility could be:
>
>     closure(foo);
>     closure($obj->Fot);
>     closure(Zoq::Fot);
>

It looks like a function but it's not a function cause what you have inside
parentheses looks like a const, property and class const.

IMO a statement like that for consistency it should be with no parentheses
like:

$foo = closure foo;
$foo = closure $obj->Fot;
$foo = closure Zoq::Fot;

Cheers,
--
Michał Brzuchalski

---

## #108499 — Dik Takken — 2020-02-11T20:11:49+00:00

URL: https://externals.io/message/108459#108499 · In reply to: #108497

> 
> 
> > […]
> 
> I don't think I meant that. I think it still should accept variables
> as well, so it would still be ambiguous as to whether it referred to
> the value of the property Fot of object $obj, or the method Fot on the
> $obj class.

In that case, the ambiguity issue remains. I was thinking about it as a
construct for wrapping functions and methods into closures. Could you
provide an example of how accepting variables would be useful?

> > […]
> 
> That has the downside of exposing the implementation detail, that it's
> implemented through closures, which would be a regrettable choice if
> we ever decided to change the implementation details in the future.

I don't think I understand. Creating a closure is something you do
because of the specific semantics they have. The fact that it yields a
closure is not a detail, it is the reason why I would use it...

> And it also has the downside of looking like a function call, but
> having different rules for what can be put inside it:
> 
> function foo();
> closure(foo); // fine.
> special_closure(foo); // syntax error, undefined constant foo.

That is a downside indeed.

Regards,
Dik Takken

---

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