You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Description
By adding simple generic annotations to the PHPDoc comments of the Promise interface, it could gain a lot of introspection abilities. Even Laravel recently implemented generic annotations, so I guess you can call them widely supported and considered useful!
The neat thing about annotated generics in PHP is that they are entirely optional. So this would not cause any backwards-incompatibility to speak of.
Example
By adding an @template T annotation to the main Promise interface, and augmenting the type of then with it, a Promise instance could be assigned a new meta property during static analysis: The value it will resolve to.
classPromiseFactory
{
/** * @template T * @param T $value * @return Promise<T> */publicstaticfunctionmake(mixed$value): Promise {
returnnewSomePromiseImplementation($value);
}
}
/** * @return Promise<string> */functionfoo(): Promise {
returnPromiseFactory::make('test');
}
// Inferred to be a string$stringValue = foo()->wait();
Additional context
I originally hit this issue when upgrading the Elasticsearch dependency in our Laravel client from 7 to 8; in that version, the client may return a response instance or a promise, if async handling is enabled.
My original plan was to add support for promises too, so the client will either perform response handling synchronously if a Response instance is returned, or do the handling inside a then callback; the client is really dependent on proper types, though, so as soon as I introduce promise support, I loose all the static analysis goodness the entire library provides.
The text was updated successfully, but these errors were encountered:
Description
By adding simple generic annotations to the PHPDoc comments of the Promise interface, it could gain a lot of introspection abilities. Even Laravel recently implemented generic annotations, so I guess you can call them widely supported and considered useful!
The neat thing about annotated generics in PHP is that they are entirely optional. So this would not cause any backwards-incompatibility to speak of.
Example
By adding an
@template T
annotation to the mainPromise
interface, and augmenting the type ofthen
with it, a Promise instance could be assigned a new meta property during static analysis: The value it will resolve to.Additional context
I originally hit this issue when upgrading the Elasticsearch dependency in our Laravel client from 7 to 8; in that version, the client may return a response instance or a promise, if async handling is enabled.
My original plan was to add support for promises too, so the client will either perform response handling synchronously if a Response instance is returned, or do the handling inside a
then
callback; the client is really dependent on proper types, though, so as soon as I introduce promise support, I loose all the static analysis goodness the entire library provides.The text was updated successfully, but these errors were encountered: