Re: [RFC] [DISCUSSION] Immutable/final/readonly properties
| From: | Máté Kocsis | Date: | Thu, 12 Mar 2020 13:57:11 +0000 |
| Subject: | Re: [RFC] [DISCUSSION] Immutable/final/readonly properties | ||
| References: | 1 2 3 4 5 6 7 8 9 10 11 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-108995@lists.php.net to get a copy of this message | ||
As you might noticed, I've not opened the vote yet. Partly because I was
improving
my implementation as well as the RFC itself (added some words about the
inheritance
implications), but the main reason is that a question arise in the
meanwhile.
Namely, "write-once" properties could in principle support covariance. That
is, a subclass
would be allowed to tighten the property type that is inherited from the
parent class.
It would be a slight change compared to regular properties that are
invariant.
All this would be possible because of the quasi-immutable nature of
"write-once" properties:
they are generally expected to be assigned to only once, in the constructor
- which is exempt from
LSP checks.
There is a gotcha though... In practice, "write-once" properties could be
written from places
other than the constructor. Although there might not be many practical
use-cases for it,
the infamous setter injection is certainly one (as shown at
https://3v4l.org/DQ3To), in which
case property covariance would be a problem.
That's why I'm curious about some additional input on the matter. Do you
think
covariance of "write-once" properties is worth to have even though there
might be
some edge-cases when it can't be supported perfectly? I'll include this
topic a bit later
in the RFC as well. In the worst case it could be added to the "Future
Scope" section
because - and correct me if I'm wrong - we can also add support for it
later since it would be
a non-breaking change.
Cheers,
Máté