-
-
Notifications
You must be signed in to change notification settings - Fork 946
Description
Support of lower bounds for generics / templates
Hi, first of all, thanks for all the great work, that has been done on generics lately.
To have generics more complete I suggest to introduce lower bounds, since phpstan already have a way to define upper bounds.
Lower bound behaves similarly to upper bound, only difference is, that it allows types in opposite direction (super types instead of sub types).
I propose syntax @template B super A with meaning B must be same type as A or super type of A.
Here is example, that I would like to support with phpstan:
https://phpstan.org/r/3fb61a92-b113-4d99-851c-25e406ea05cc
I am not good in making simplistic examples, so if anyone has better (simpler) example to show, please add it here.
Lower bounds are supported in other languages, which supports generics, e.g.:
- hack: https://docs.hhvm.com/hack/generics/type-constraints (also uses
superkeyword) - scala: https://docs.scala-lang.org/tour/lower-type-bounds.html
Here is BTW link to our library, where we would use it https://github.com/bonami/collections e.g. here bonami/collections@a0bd909#diff-5d09c8cea366687b9742aed7f42996c7d73643bc15c4cae25f21e474c7881341R712 (we cannot define ArrayList template as covariant because of this limitation, which has other impacts)