Re: [PHP-DEV] [RFC]

This is only part of a thread. view whole thread
  108558
February 14, 2020 00:39 mike@newclarity.net (Mike Schinkel)
> On Feb 13, 2020, at 7:24 PM, Rowan Tommins collins@gmail.com> wrote: > > An idea I had earlier which might solve some of them is if what was returned was not a normal Closure instance, but a new class like FunctionReference. It could then "remember" the name of the function wrapped, and implement __toString, Serializable, etc. It could inherit from Closure, so instanceof checks would work, and bindTo would return a normal Closure. I'm sure there's downsides I haven't thought of yet, but I thought I'd throw the idea into the mix.
I thought about that too, and mentioned it yesterday in a reply[1] to you on this list. Here is the link to the Gist with the hypothetical code using such a concept: - https://gist.github.com/mikeschinkel/78684d708358e1d101e319c7a2fdef9c -Mike [1] https://www.mail-archive.com/internals@lists.php.net/msg100719.html
  108559
February 14, 2020 07:04 manuelcanga@gmail.com (Manuel Canga)
On Fri, 14 Feb 2020 at 01:39, Mike Schinkel <mike@newclarity.net> wrote:

> > On Feb 13, 2020, at 7:24 PM, Rowan Tommins collins@gmail.com> > wrote: > > > > An idea I had earlier which might solve some of them is if what was > returned was not a normal Closure instance, but a new class like > FunctionReference. It could then "remember" the name of the function > wrapped, and implement __toString, Serializable, etc. It could inherit from > Closure, so instanceof checks would work, and bindTo would return a normal > Closure. I'm sure there's downsides I haven't thought of yet, but I thought > I'd throw the idea into the mix. > > I thought about that too, and mentioned it yesterday in a reply[1] to you > on this list. > > Here is the link to the Gist with the hypothetical code using such a > concept: > > - https://gist.github.com/mikeschinkel/78684d708358e1d101e319c7a2fdef9c > > -Mike > > > [1] https://www.mail-archive.com/internals@lists.php.net/msg100719.html > > Maybe, bettern fn: * fn(callable $to_clsoure ): *closure ?
I know fn is used in arrow functions. However, for that same reason fn is the most convenient. Using dik examples( by the way, they are brilliant ): $result = Stats::of($string) ->analyze(fn(normalizeNewlines)) ->analyze(fn(readingLevel)) ->analyze(fn(countStats)) ->analyze(fn($synonymSuggester, 'analyze')) ->analyze(fn(WordCouter::class, 'analyze')) ->analyze(fn($s) => wordDistribution($s, 3)) Returning to ::function again and with a modified example of dik( again ): Now is: result = Stats::of($string) ->analyze('\Stats\Analyzer\normalizeNewlines') ->analyze('\Stats\Readings\readingLevel') ->analyze('\Stats\Parser\countStats') ->analyze(fn($s) => \Stats\Analyzer\wordDistribution($s, 3)); Could be: use function Stats\Analyzer\normalizeNewlines; use function \Stats\Readings\readingLevel; use function \Stats\Parser\countStats; result = Stats::of($string) ->analyze(normalizeNewlines::function) ->analyze(readingLevel::function) ->analyze(countStats::function ) ->analyze(fn($s) => \Stats\Analyzer\wordDistribution($s, 3)); Maybe '::function' is something long, however it wiil be autocompleted by editors and, moreover, nowadays function is a reserved keyword. Could we use "normalizeNewlines::name", but in this case: normalizeNewlines is function or class ?, because in this case could be "normalizeNewlines" a class and "name" a constant of class. Rowan put me in an awkward situation with [MyClass::class, method::function ] however, in this case, could be simply MyClass::class, 'method' ]. Because method don't need be used with namespace. ::function could be documented with advice of being used with name of functions. Because ::function is also useful in array: $filters = [ normalizeNewlines::function, readingLevel::function, countStats::function ] $content = Stats::of($string); foreach($filters as $filter) $filter($content); Regards
  108593
February 15, 2020 08:22 larry@garfieldtech.com ("Larry Garfield")
On Fri, Feb 14, 2020, at 1:04 AM, Manuel Canga wrote:

> Maybe, bettern fn: * fn(callable $to_clsoure ): *closure ? > > I know fn is used in arrow functions. However, for that same reason fn is > the most convenient. > > Using dik examples( by the way, they are brilliant ):
Point of order: The Stats analyzer examples were from me, not Dik. :-) They're taken from one of the monad chapters in the book I'm working on. --Larry Garfield
  108594
February 15, 2020 11:51 php@manuelcanga.dev (Manuel Canga)
---- En sáb, 15 feb 2020 09:22:41 +0100 Larry Garfield <larry@garfieldtech.com> escribió ----
 > On Fri, Feb 14, 2020, at 1:04 AM, Manuel Canga wrote:
 > 
 > > Maybe, bettern fn:  * fn(callable $to_clsoure ): *closure ?
 > > 
 > > I know fn is used in arrow functions. However, for that same reason fn is
 > > the most convenient.
 > > 
 > > Using dik examples( by the way, they are brilliant ):
 > 
 > Point of order: The Stats analyzer examples were from me, not Dik. :-)  They're taken from one of the monad chapters in the book I'm working on.
 > 
 > --Larry Garfield


Oops!, sorry Larry!. Then you book will be securily a great book.
  108595
February 15, 2020 12:07 php@manuelcanga.dev (Manuel Canga)
---- En sáb, 15 feb 2020 09:22:41 +0100 Larry Garfield <larry@garfieldtech.com> escribió ----
 > On Fri, Feb 14, 2020, at 1:04 AM, Manuel Canga wrote:
 > 
 > > Maybe, bettern fn:  * fn(callable $to_clsoure ): *closure ?
 > > 
 > > I know fn is used in arrow functions. However, for that same reason fn is
 > > the most convenient.
 > > 
 > > Using dik examples( by the way, they are brilliant ):
 > 
 > Point of order: The Stats analyzer examples were from me, not Dik. :-)  They're taken from one of the monad chapters in the book I'm working on.
 > 
 > --Larry Garfield
 > 

By the way...the following is for folks in internals:

Do you think it is worth creating an RFC page about :: function ? I say that because I see that there are many voices against of :: function
  108601
February 15, 2020 17:01 larry@garfieldtech.com ("Larry Garfield")
On Sat, Feb 15, 2020, at 6:07 AM, Manuel Canga wrote:
> > ---- En sáb, 15 feb 2020 09:22:41 +0100 Larry Garfield > <larry@garfieldtech.com> escribió ---- > > On Fri, Feb 14, 2020, at 1:04 AM, Manuel Canga wrote: > > > > > Maybe, bettern fn: * fn(callable $to_clsoure ): *closure ? > > > > > > I know fn is used in arrow functions. However, for that same > reason fn is > > > the most convenient. > > > > > > Using dik examples( by the way, they are brilliant ): > > > > Point of order: The Stats analyzer examples were from me, not Dik. > :-) They're taken from one of the monad chapters in the book I'm > working on. > > > > --Larry Garfield > > > > By the way...the following is for folks in internals: > > Do you think it is worth creating an RFC page about :: function ? I say > that because I see that there are many voices against of :: function
There seems to be little pushback on the idea of a better way to name/reference functions, so an RFC page for that seems reasonable. What the syntax is, that's still in heavy bikeshed territory, so maybe don't name the RFC based on the implementation detail yet. :-) I'd love to get some input from Nikita or Dimitry or someone else with way more parser experience on how feasible any of the options discussed so far would be; if some are vastly more straightforward than others, that carries a lot of weight. --Larry Garfield
  108611
February 15, 2020 23:25 d.h.j.takken@freedom.nl (Dik Takken)
On 15-02-2020 13:07, Manuel Canga wrote:
> Do you think it is worth creating an RFC page about :: function ? I say that because I see that there are many voices against of :: function
Let us go back to the start of this thread. Your idea was to simplify array_map('\My\I18N\i18n_translate', $array) to array_map(i18n_translate::func, $array) Then the discussion took the topic one level deeper. In your examples, you seek a better way to pass a function as callable. In PHP a callable can be: 1. a string 2. an array 3. a closure Then it was suggested that a short syntax to wrap the function into a closure is more useful. Using strings to refer to functions has consistency issues and the proposed ::func does not solve them. Also, the array syntax for referring to methods could use a better alternative. All things considered, I think the originally proposed feature, using ::func to get the function name, has limited use. Having a ::func (or ::function or ::fn) return a closure might be somewhat controversial and there is no consensus about the name. The only way to know how this will be received is to write an RFC and call for a vote. My own guess would be that an 'enclosure' construct along the lines of array_map({i18n_translate}, $array) array_map({$object->method}, $array) has better chances of succeeding but you never know what happens. Perhaps you could team up with Michał and use the feedback from this thread to produce an RFC. Regards, Dik Takken
  108560
February 14, 2020 08:23 rowan.collins@gmail.com (Rowan Tommins)
On 14 February 2020 00:39:15 GMT+00:00, Mike Schinkel <mike@newclarity.net> wrote:
>> On Feb 13, 2020, at 7:24 PM, Rowan Tommins collins@gmail.com> >wrote: >> >> An idea I had earlier which might solve some of them is if what was >returned was not a normal Closure instance, but a new class like >FunctionReference. It could then "remember" the name of the function >wrapped, and implement __toString, Serializable, etc. It could inherit >from Closure, so instanceof checks would work, and bindTo would return >a normal Closure. I'm sure there's downsides I haven't thought of yet, >but I thought I'd throw the idea into the mix. > >I thought about that too, and mentioned it yesterday in a reply[1] to >you on this list. > >Here is the link to the Gist with the hypothetical code using such a >concept: > >- https://gist.github.com/mikeschinkel/78684d708358e1d101e319c7a2fdef9c
What I had in mind was a combination of that and the existing Closure class, so: $ref = foo::fn; $ref(); // run the function $ref->name; // access extra metadata $ref->bindTo($whatever); // get a new Closure with a bound context That would also combine well with one of the proposed bracket style syntaxes that let you specify methods more naturally: {foo}->name; // qualified name of local function {Foo::bar}; // closure for a static method {$this->baz}->bindTo($that); // closure for a method of current class rebound to a different object Regards, -- Rowan Tommins [IMSoP]