Re: [Concept] Extension methods

From: Date: Thu, 11 Aug 2022 18:42:30 +0000
Subject: Re: [Concept] Extension methods
References: 1 2 3  Groups: php.internals 
Request: Send a blank email to internals+get-118416@lists.php.net to get a copy of this message
On Thu, Aug 11, 2022, at 4:03 AM, Alex Wells wrote: > Besides, I believe working with existing functions is more of a problem > than a bonus - because of the naming. You’d have to clutter your code > with array_map calls, even though it could easily be shortened > to > map. A couple of trivial higher order functions and you're good to go. I have a sampling of them here: https://github.com/Crell/fp/blob/master/src/array.php (Modulo PHP's inconsistency between internal/user-space functions that force having separate functions for keyed and unkeyed arrays; any solution is going to have to deal with that problem.) >> On 11 Aug 2022, at 07:36, Paul Crovella <paul.crovella@gmail.com> wrote: >> >> This is essentially making -> the pipe operator with >> extra steps (extension/use >> extension) and less utility (not working on existing functions.) > > Well, pipe operator is another option, but it’s got it’s downsides > compared to extension methods: > - it's less versatile: extension methods are required to specify a > type they’re extending, meaning they are methods, not functions. Hence, > two different map method extensions can be imported in a single > file > (given they’re for different types - say one for Collection, > the > other for array), unlike regular functions. I believe it’s > common > place to use both Collection::map and ©µòÕùíGhXz > K!ÊP®array::map in a single file, > but that wouldn’t be possible or would require aliasing > - it’s uglier: since it just uses functions, to avoid clashes between > same method names, prefixes would be required Flipside: Pipe works on arrays and strings, which this would not. And arrays and strings are among the most common things to be chained in this way. (Most Collection objects are just alternate OOPy syntax around array_map and array_filter, at the end of the day.) Pipe also doesn't give the impression that the method is "part of" the object (it's not), whereas extensions do, despite being effectively just an alternate syntax for a public function that takes the object as an argument. And functions can be easily namespaced. --Larry Garfield

« previous php.internals (#118416) next »