[RFC] Callable Types

php.internals

Marcio Almada

10 years ago
Redirecting this to the list as this message was sent privately probably by accident :) ---------- Forwarded message ---------- From: Mathieu Rochette <mathieu@texthtml.net> Date: 2016-04-23 12:44 GMT-04:00 Subject: Re: [PHP-DEV][RFC] Callable Types To: Marcio Almada <marcio.web2@gmail.com> On 04/22/2016 06:12 AM, Marcio Almada wrote: Hello everyone, We just completed the draft for the "Callable Types" RFC. This RFC has been recently mentioned during other type related threads on this mailing list, so it feels to be the right time to put the proposal in context: The proposal is at https://wiki.php.net/rfc/callable-types It looks very nice overall :) and would be, imho, a great addition to PHP I have a few comments, first about the optional arguments, the example in the rfc says :
> // That means that foo() could call $cb and pass anything as a first
argument and if it would be something that is not an instance of A the call would fail. I don't get why foo would be declared as function foo(callable() $cb) { } instead of function foo(callable($any) $cb) { } if foo intends to call $cb with an argument. what about optional arguments, eg: class Foo { private $logger; public __construct(Logger $logger = null) {$this->logger = $logger;} public doSomething(callable(int $done, int $remaining, Logger = null) $progressCb) { // ... for($i = 0; $i < $n; $i++) { // ... $progressCb($i, $n - $i, $this->logger); } } } should that be supported ? and last one, will instanceof supports this new syntax? thank you The W.I.P patch is available for testing through http://3v4l.org under the RFC tab. We count with your detailed feedback and insights. Let's have a nice, respectful and constructive conversation about the RFC and work on possible improvements! Thanks Nikita Nefedov Márcio Almada
-- Mathieu Rochette

Marcio Almada

10 years ago
hi! From: Mathieu Rochette <mathieu@texthtml.net>
> Date: 2016-04-23 12:44 GMT-04:00 > Subject: Re: [PHP-DEV][RFC] Callable Types > > > On 04/22/2016 06:12 AM, Marcio Almada wrote: > > Hello everyone, > > We just completed the draft for the "Callable Types" RFC. This RFC has been > recently mentioned during other type related threads on this mailing list, > so it feels to be the right time to put the proposal in context: > > The proposal is at https://wiki.php.net/rfc/callable-types > > It looks very nice overall :) and would be, imho, a great addition to PHP > > > I have a few comments, first about the optional arguments, the example in > the rfc says : > > // That means that foo() could call $cb and pass anything as a first > argument and if it would be something that is not an instance of A the call > would fail. > I don't get why foo would be declared as > > function foo(callable() $cb) { } > > instead of > > function foo(callable($any) $cb) { } > > if foo intends to call $cb with an argument. > > > what about optional arguments, eg: > > class Foo > { > private $logger; > public __construct(Logger $logger = null) {$this->logger = $logger;} > public doSomething(callable(int $done, int $remaining, Logger = null) > $progressCb) { > // ... > for($i = 0; $i < $n; $i++) { > // ... > $progressCb($i, $n - $i, $this->logger); > } > } > } > > should that be supported ? > >
Currently you can nullify or skip the Logger argument upon implementation of the callable. Rendering your example: https://3v4l.org/qb5QR/rfc#tabs Admittedly, the expectation for default values to work seems legit and we are currently discussing whether to support default values on callable types signatures or not (inclined to do not support). But honestly I haven't seen a legit use case when we already have the ability to satisfy the callback with a smaller type. Perhaps you may have one use case where a default value would be indispensable?
> > and last one, will instanceof supports this new syntax? > >
Yes, please expect to see this on the next minor version of the RFC :)
> > thank you > > The W.I.P patch is available for testing through http://3v4l.org under the > RFC tab. > > We count with your detailed feedback and insights. Let's have a nice, > respectful and > constructive conversation about the RFC and work on possible improvements! > > Thanks > Nikita Nefedov > Márcio Almada > > > -- > Mathieu Rochette > >
Cheers.

Mathieu Rochette

10 years ago
On 04/23/2016 08:34 PM, Marcio Almada wrote:
> hi! > > From: Mathieu Rochette <mathieu@texthtml.net> >> Date: 2016-04-23 12:44 GMT-04:00 >> Subject: Re: [PHP-DEV][RFC] Callable Types >> >> >> On 04/22/2016 06:12 AM, Marcio Almada wrote: >> >> Hello everyone, >> >> We just completed the draft for the "Callable Types" RFC. This RFC has been >> recently mentioned during other type related threads on this mailing list, >> so it feels to be the right time to put the proposal in context: >> >> The proposal is at https://wiki.php.net/rfc/callable-types >> >> It looks very nice overall :) and would be, imho, a great addition to PHP >> >> >> I have a few comments, first about the optional arguments, the example in >> the rfc says : >>> // That means that foo() could call $cb and pass anything as a first >> argument and if it would be something that is not an instance of A the call >> would fail. >> I don't get why foo would be declared as >> >> function foo(callable() $cb) { } >> >> instead of >> >> function foo(callable($any) $cb) { } >> >> if foo intends to call $cb with an argument. >> >> >> what about optional arguments, eg: >> >> class Foo >> { >> private $logger; >> public __construct(Logger $logger = null) {$this->logger = $logger;} >> public doSomething(callable(int $done, int $remaining, Logger = null) >> $progressCb) { >> // ... >> for($i = 0; $i < $n; $i++) { >> // ... >> $progressCb($i, $n - $i, $this->logger); >> } >> } >> } >> >> should that be supported ? >> >> > Currently you can nullify or skip the Logger argument upon implementation > of the callable. Rendering your example: > > https://3v4l.org/qb5QR/rfc#tabs > > Admittedly, the expectation for default values to work seems legit and we > are currently discussing whether to support > default values on callable types signatures or not (inclined to do not > support). But honestly I haven't seen a legit use > case when we already have the ability to satisfy the callback with a > smaller type. Perhaps you may have one use case > where a default value would be indispensable?
I wouldn't say it's indispensable. it's just that in the previous example, the class Foo cannot say that the #rd argument of the callable is either a Logger or null. that means that giving function(int $done, int $remaining){var_dump($remaining);} works but function(int $done, int $remaining, Logger $logger){$logger->debug($remaining);} does not even though it is compatible with the callable signature. and afaikt, I cannot make a signature without a default null value or one of the rfc about union types or nullable types
> > >> and last one, will instanceof supports this new syntax? >> >> > Yes, please expect to see this on the next minor version of the RFC :)
great ! I've seen someone mentioning variadics so I made a few small tests, I think those two should work (they currently don't): https://3v4l.org/eZgR9/rfc#rfc-callable_typehint https://3v4l.org/N7i0u/rfc#rfc-callable_typehint and this one should not: https://3v4l.org/fcRlT/rfc#rfc-callable_typehint
> > > >> thank you >> >> The W.I.P patch is available for testing through http://3v4l.org under the >> RFC tab. >> >> We count with your detailed feedback and insights. Let's have a nice, >> respectful and >> constructive conversation about the RFC and work on possible improvements! >> >> Thanks >> Nikita Nefedov >> Márcio Almada >> >> >> -- >> Mathieu Rochette >> >> > Cheers. >
-- Mathieu Rochette