On Thu, Apr 20, 2023, at 8:26 PM, Michał Marcin Brzuchalski wrote: > Hi > >> There is one loophole, in that an interface may require an __invoke() >> method: >> >> interface TwoInts >> { >> public function __invoke(int $a, int $b): int; >> } >> >> > I was playing around with the code and parser for this in 2020 but my idea > was to introduce a new syntax that is inspired by C# - Delegates [1] > > delegate Reducer (?int $sum, int $item = 0): int; > > class Foo implements Reducer { > public function __invoke(?int $sum, int $item = 0): int { } > } > function reduce(Reducer $reducer) { > var_dump($reducer(0, 5)); > } > reduce(new Foo()); > reduce(fn(?int $sum, int $item = 0): int => 8); > > At the same time, I assumed structural typing for closures would be used. > I assumed the delegate will resolve into > interface Reducer { > public function __invoke(?int $sum, int $item = 0): int {} > }
This is effectively the same as the "typedef" version of callable types from my email to Dan a moment ago. See there for the challenges. --Larry Garfield -- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: https://www.php.net/unsub.php