Open kkmuffme opened 1 year ago
I found these snippets:
Your two first link shows exactly the same errors
Sorry, copied the wrong one, fixed now.
@readonly @mutation-free
: https://psalm.dev/r/232ed66d3b
@immutable
: https://psalm.dev/r/5c9610e802
Both show the same (aka no) errors.
You had a @pure
on the method which caused the errors...
I found these snippets:
I know that the @pure caused the errors, but why are the errors different depending on whether I use immutable on the class or readonly/mutation-free on all properties?
bcs you can actually 'use' an immutable object in a pure context.
Since the @immutable
annotation gives psalm the guarantee that really all properties are readonly and all methods do not mutate, the ImpurePropertyFetch
issue will be gone.
Used to annotate a class where every property is treated by consumers as @psalm-readonly and every instance method is treated as @psalm-mutation-free
The docs never claim, that it is the same to use either @immutable
or @readonly @mutation-free
, but state that using @immutable
will make every property be treated like having the @readonly
and every method like having @mutation-free
.
BUT: Using @immutable
gives knowlege about the 'bigger picture', therefore psalm does know more about the class itself.
Another difference is, if you use @immutable
, all child classes need to be marked as @immutable
, too.
But it seems this isn't true: https://psalm.dev/r/2d7cd6c403 When using with immutable, the errors are different: https://psalm.dev/r/c68a1c73d6
You could argue: the class might be extended and then that wouldn't be true anymore - however this is irrelevant for what is in the class for now, and has to be adjusted in the extending class. However, adding
final
to disallow extending, still gives the same errors: https://psalm.dev/r/67c22e0ca6