-
Notifications
You must be signed in to change notification settings - Fork 65
Allow [] after generic type or array shape - closes #36 #37
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
@JanTvrdik Can you review this too since you raised some concerns?
e2b8faf
to
45783e7
Compare
I hate it. I want the ambiguous []
syntax to die.
The most practical mess will be probably with callable types, because callable(): string[]
will now be parsed as array of callables returning string.
Lol, I'll add a test for that to be sure. I still don't see how []
are ambiguous. They're not more ambiguous than any other postfix operator. I don't like the fact that it defaults to TKey
being array-key
but I don't think the syntax will go anywhere soon.
I don't like the fact that it defaults to TKey being array-key but I don't think the syntax will go anywhere soon.
I don't know what you're talking about, can you post an example?
The callable example is bad and should be solved in an elegant way (so that it means "callable that returns array<string>
"). But it's still a competetive disadvantage that PHPStan doesn't understand this syntax now, so I'd like to solve it, albeit with compromises.
I've given it a little bit more thought and the problem with this implementation is that it does more than what the title says:
Allow [] after generic type or array shape
It tries to parse []
after everything, which has the problem of ambiguity. So if we limit it only to generics and array shapes for now, it should be fine?
I still don't see how [] are ambiguous
Not syntactically ambiguous, but semantically ambiguous. Foo[]
can mean either array<Foo>
, iterable<Foo>
of generic type (when used as part of union type).
Yeah, but we will have to live with that. It's like wishing IPv4 to die...
I don't know what you're talking about, can you post an example?
What I meant is just that I'd prefer []
to be a packed array by default, so what Psalm considers to be a list. But that would make 90% of the array annotations in the wild incorrect.
It tries to parse [] after everything
Anything that is atomic. The problem is that callables aren't atomic. There's still a PR from @JanTvrdik to solve that.
I'll merge this if you do it only for generics and array shapes + write a test case that shows that callable(): string[]
didn't change :) It has the highest potential of not breaking anything. Thanks.
552a664
to
93065d1
Compare
Done. I also noticed the following:
callable(): int[] // was parsed as callable: int $this[] // was parsed as $this ?int[] // was parsed as ?int, is now array<int>|null analogous to Psalm
* Array shapes * Generics * $this * Nullables
93065d1
to
e0e5c18
Compare
Thank you very much!
@ondrejmirtes You're welcome 🙂
No description provided.