-
Notifications
You must be signed in to change notification settings - Fork 65
(RFC) Implement template default syntax #148
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
71d2dac
to
3c43569
Compare
/cc @orklah Does Psalm already have this / would you be interested too?
orklah
commented
Sep 6, 2022
Psalm doesn't have this but it could be interesting (especially because Psalm sometimes struggle to assign values to classes with templates that were never properly created)
I doubt I'll be in position to implement this myself anytime soon though
Alright, I'm fine with going ahead with this. But what the syntax should be? The issue proposes a separate @template-default
tag. What's the precedent from other languages that have this feature?
How would the syntax proposed here for @template
work when the bound is involved?
I've taken the syntax from TypeScript. At least C++ also uses this syntax, but I haven't looked at other languages.
Because the default value is tightly coupled to the type parameter (just like the bound is), I don't think it's a good idea to have a separate tag for it.
Nice, it makes sense to me. Could you also please submit a proof of concept to phpstan-src to make sure these are not released with a big time gap between them? :) Thank you.
What we need to think about is modifying the rules here https://github.com/phpstan/phpstan-src/tree/1.8.x/src/Rules/Generics for this new feature:
- We need to allow to omit the optional template type around here https://github.com/phpstan/phpstan-src/blob/fb08fb530f60d3510f2864b9935d2f75b29d7840/src/Rules/Generics/GenericObjectTypeCheck.php#L60-L78
- We need to check that the default type is a subtype of the bound in this class https://github.com/phpstan/phpstan-src/blob/1.8.x/src/Rules/Generics/TemplateTypeCheck.php
- We need to check that an optional template type is not followed by a non-optional one, also probably in TemplateTypeCheck
96c7c66
to
f3e2a98
Compare
Thank you!
alexander-schranz
commented
Oct 14, 2022
Nice to see this merged. 🎉 🚀
Proof-of-concept implementation for template default syntax (for phpstan/phpstan#4801)