Re: [Discussion] Callable types via Interfaces

From: Date: Thu, 20 Apr 2023 20:49:35 +0000
Subject: Re: [Discussion] Callable types via Interfaces
References: 1 2  Groups: php.internals 
Request: Send a blank email to internals+get-120090@lists.php.net to get a copy of this message
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

« previous php.internals (#120090) next »