-
Hi, I am trying to create a generic trait that allows me to create an object based on the items passed. I found out that psalm does not allow to use the class template in static functions, however I don't really understand why. Just using template on the static function doesn't help me, as the class using the trait specifies the trait. Here is an example on what I am trying to do: https://psalm.dev/r/212267eb8a Any help is much appreciated! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
The basic principle is that class level templates are used to describe the state of a specific instance of a class (For example, you can have a By that definition, it makes little sense to have a static method using a class level template because the method doesn't belong to a specific instance so the template could not have been initialized. However, we know that this definition is restrictive and for cases like yours (where the class level template is not designed to describe the content of a specific instance but rather the behavior of the class itself), this explanation doesn't fit. Historically, there was a lot of confusion in users that didn't understood the difference between class level templates and method level templates and they kept reporting bugs for that. Matt decided to restrict the use of class level templates on static methods but it did leave a hole in Psalm's coverage of use-cases. I wish we had another tag for class level templates that would be different from templates that describe instances to cover this use case but for now we don't |
Beta Was this translation helpful? Give feedback.
The basic principle is that class level templates are used to describe the state of a specific instance of a class (For example, you can have a
Collection<Cat>
in some variable and aCollection<Dog>
in another.) You get to that state by pushing an array of Dogs in a Collection and an other array of Cats in another.By that definition, it makes little sense to have a static method using a class level template because the method doesn't belong to a specific instance so the template could not have been initialized.
However, we know that this definition is restrictive and for cases like yours (where the class level template is not designed to describe the content of a specific instance but ra…