Re: [Concept] Extension methods

From: Date: Thu, 11 Aug 2022 19:07:20 +0000
Subject: Re: [Concept] Extension methods
References: 1 2 3 4 5 6  Groups: php.internals 
Request: Send a blank email to internals+get-118422@lists.php.net to get a copy of this message
> On 11 Aug 2022, at 21:58, Larry Garfield <larry@garfieldtech.com> wrote: > > On Thu, Aug 11, 2022, at 1:51 PM, Alex Wells wrote: > >> The pipe operator RFC has actually been mentioned before; the short >> takeway is: pipe operator works and has a benefit of using existing >> functions. The downside is that those are still functions, not methods, >> meaning you’ll still be left with [your_type]_ prefixes >> for all >> function names, unlike extension methods, where you can have multiple >> methods with the same name (for different types). > > This is incorrect, and has been since functions could be namespaced. Please stop repeating > this, it is FUD. > > Also, "existing functions" is also not quite accurate. PIpe worked with any > callable; the Partial Function Application RFC was intended to make turning arbitrary functions into > callables, which could then be easily piped, but you can also use arbitrary callables, including > closures and objects that implement __invoke(). array_map in its current form wouldn't be > pipeable anyway as it requires multiple arguments, but higher order functions that are pipe-friendly > as trivial to write. (See previous message for a link to many.) > > --Larry Garfield > > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: https://www.php.net/unsub.php > Functions can be namespaced indeed. I’m referring to another thread where I brought an example with Collection::map and array::map. You can easily define those as functions in different namespaces, but when it comes time to use it, you’ll likely end up having to use the same function name in a single file. It’s then one of three options: 1) you don’t import your functions, leading to ugly long calls: \Illuminate\Collection\map(…, fn () ..), \Some\Vendor\Arrays\map(…, fn () ..) 2) you do import your functions, but end up aliasing: use function Illuminate\Collection\map as collection_map;, use function Some\Vendor\Arrays\map as array_map; 3) you define your functions with prefixes from the beginning, just to avoid having to manually alias them all the time: use function Illuminate\Collection\collection_map; All of the solutions are far from perfect. I apologize if I’m missing something.

« previous php.internals (#118422) next »