-
-
Notifications
You must be signed in to change notification settings - Fork 863
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
@template annotation syntax issues #2527
Comments
Overall, I absolutely endorse having a As for the naming "template", I'm not super worried about it, but the community does indeed call them "generics". |
Here is a suggestion that attempts to address the 4 mentioned issues:
Examples:
Example on a class:
Pros:
|
In the PHP world I think How about cc @TysonAndre who works on Phan. |
Thanks for adding Tyson. I like A synonym for "parameter type" is "type variable". This gives us an other possible alternative: |
The largest issue is doctrine - Symfony can be taken care of by being case sensitive for non-standard tags (requiring The same issue with I like Another way to solve this would be to add a common prefix that all static analyzers (or IDEs) could use, if a syntax and meaning could be agreed on:
|
What about |
|
The
|
Provided that we absolutely need hyphens in order to escape the Doctrine annotation parser, this could be a solution. The prefix could be optional, so that both |
I also like
|
The one-line
You can still write something like The only disadvantage I can think of is that it condenses a lot of information into a single line in case of more complex definitions. And that there is no |
/**
* @generic<
* T is \Foo\Bar\Baz,
* U is \Foo\Bar\Baz,
* >
*/
class Foo {} Or we'd could do something like /**
* @generic<T, U>
* @generic-constraint T is \Foo\Bar\Baz
* @generic-constraint U is \Foo\Bar\Baz
*/
class Foo {} which is something Swift and Rust do for readability: fn foo<T, U>() -> T
where
T: Bar + Baz,
U: Baz,
{} |
I would like to add that we also need to support variance modifiers in the syntax. I personally like Kotlin's With
The The |
Heads up: Psalm already implements covariant parameters using |
If we end up with a whole family of
Not sure this makes any sense to PHP developers... Is it really necessary for the user to specify this? Does it even make sense for the user to have control over whether to allow covariant / contravariant types? IIUC the return type is always covariant, and the parameter type is always contravariant: https://wiki.php.net/rfc/covariant-returns-and-contravariant-parameters |
I mentioned this to Ondrej a couple of weeks ago, but another reason I think a single line i.e. they don't write And it's very rare that there are more than two template params for a given class. I've never heard of users being tripped up by the order of them. Re covariance:
Yes. It avoids a whole class of bugs - see vimeo/psalm#1603 I'm less worried about template contravariance (Psalm doesn't yet support it). |
We've decided to try out |
Oops... I didn't read properly. What I'm suggesting is to rename
Can't the static analyzer tell that it's type narrowing (thus not allowed) when you try to pass |
You could use a Twitter poll. |
The idea with |
|
Absolutely, but there's no reason to assume the sample would be skewed one way or the other - as long as the sample is representative there shouldn't be a problem.
Asking people to choose between two different ways of expressing the same thing in their codebase (and explaining that via documentation) is much worse, IMO, than using the results of a poll. Presumably you'll document the use of generics/templates, and your examples of templated behaviour will choose one over the other. That's very likely the one your users will use. |
I just believe the correct feedback loop is only established by releasing something for real instead of just asking/trying out something artificially... |
But users will just use whichever one is preferred by the documentation. People don't like to have to think about that sort of stuff. I'm unaware of a time TypeScript ever launched a feature and told their users "we're giving you the choice of syntax – if you don't use version A we'll deprecate it in the future". Either you introducing |
Yes, thanks :) We will introduce it but it's possible that people won't like it and will stay with We'll also probably use |
Well now you've gone too far ;) |
Will |
We currently only support class names as bounds but we want to expand that to |
It could, but it would result in unstable APIs |
Hi, we'd like to do |
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
I would like to relaunch the discussion around the
@template
annotation, since multiple issues have been raised around its syntax that may justify a change. Such a change would be relatively easy before the feature is shipped in phpstan-stable.Here is a summary of the issues that have been raised in the past:
Semantic mismatch (vs C++)
The name
template
refers to C++-style generics, whose semantics do not match PHPStan or Psalm semantics (who are closer to Java-style generics). It can be misleading and give wrong expectations.Naming conflict
The name
template
might conflict with existing projects already using an annotation named like this (for example, Symfony uses an annotation with that name do indicate which HTML template should be used to render a controller). Because of this, PHPStan/Psalm might be confused when analyzing these projects, and these projects might be confused when importing phpstan/psalm-annotated code.Doctrine parser
The Doctrine annotation parser will attempt to load any annotation whose name looks like a valid class name (
template
does). Because of this, projects that use this parser may fail when using phpstan/psalm-annotated code. (Although it is possible to tell the parser to ignore some annotations. The ignore list is case-sensitive, so it is possible to ignore phpstan'stemplate
while not ignoringTemplate
.)This issue actually exists for all non-standard phpDoc annotations.
of
/as
keywordsIt has been suggested to replace the
of
andas
keyword used for declaring an upper bound for types, byextends
.Previous discussions
Here are previous discussions I can remember:
cc @mindplay-dk @muglug @Ocramius @ondrejmirtes @JanTvrdik @hrach @teohhanhui
The text was updated successfully, but these errors were encountered: