Replies: 2 comments 3 replies
-
Ok, the wat part is probably a bug. I think your use case is not possible because Psalm forbid you to use class level templates on static methods. It used to be possible but it was removed because there was a lot of confusion by users. The rationale was that class level templates are inherently linked to a specific instance so it should not come up in a static method in theory However, there are some usages where templates are not used as a way to document the state of an instance like your use case with a Collection of entities |
Beta Was this translation helpful? Give feedback.
-
Rather than starting a new thread, I thought I'd re-use this one to look at another challenge when using generics https://psalm.dev/r/f0fc33f01b In this structure we have a set of services, which all extend each other, and a set of factories to create said services, which also all extend each other. There's various ways I've tried passing the templates down, but always seem to get invalid arguments because it won't expand to cover the fact the generics are child classes. Am I understanding generics wrong, or is this another Psalm limitation as far as inheritance goes? |
Beta Was this translation helpful? Give feedback.
-
The query is explained here: https://psalm.dev/r/0ba43859fc
This is a case where we want a generic collection that we can make specific versions of. I.e. for a specific version we want to constrain the type T to a given entity type.
I am confused about the UnsafeGenericInstantiation warning given here - as far as I can see this isn't unsafe, as it can only be passed through as T, and if T is constrained, only the given value of T would be permitted in the static factory method.
An additional oddity, is that if I don't add the line
@template Wat
I get another error thatT
is undefined - however addingWat
without any other reference to it seems to remove that. However I wonder are these two related, and the static issue here is that it can't link the template of the underlying class.Any pointers would be appreciated because I am going out of my mind trying to make this concept work!
Beta Was this translation helpful? Give feedback.
All reactions