> Well, I don’t know. Everyone seems to think of init hooks (and their playing > together with other hooks) differently. > Some say this, some say that. That’s the exact issue. Want an example? > > Eric just agreed with your code example which has a get hook AND init hook. > > ``` > class Test { > public int $seek { > init => random_int(0, 100); // called once on read if not > initialized > get => $this->seek + 100; > set => $this->seek = $value + 100; > } > } > ``` > > var_dump((new Test())->seek); // number between 100-200 OR 200-300 depending > of the set hook be called once with the result of init as well. > > Or did I misunderstand it? > > > I share the same understanding as you, Marc. > > > But one mail before he answered to Larry: > > I think, at least for readonly, you couldn't have an init hook and a > get hook, since the main objection here is to having get hooks on > readonly properties at all. On normal properties, I think that'd be > okay? > > > So what is it? Get hook cool, or not? And how does an init hook work exactly? > How play all combinations together? And how with readonly? > Why didn’t your example use readonly if we talk about readonly hooks?
I believe you are attempting to point out a contradiction, but as far as I can tell my two statements are consistent - his example did not include readonly, and it makes sense to me as-is. My prior statement to Larry is "I think, at least for readonly, you couldn't have an init hook and a get hook". Again, his example is not readonly, so it's consistent. > Eric, > why it wouldn’t be solved by an init hook? Well, because as you said, > readonly get hooks would not be allowed in combination with init. > Others apparently have different opinions. So will they, will they not? I don't think others have different opinions, or at least I haven't heard someone say that readonly get hooks should be allowed with init. > > I, however, want get/set on readonly properties. And that is what I proposed > here. > > An init hook is not part of this proposal and I am not planning to take this > on. > This RFC, however, would not block anyone from creating their own RFC for > init hooks. > > B) Less Code > > Marc was talking about init, cache modifier and attributes in the same time. > Claude initially wanted a cache modifier on each hook. > > I argue that “readonly implicates cached” is very reasonable here. > > Many, many opinions. Many, many options. All have their pros, and cons. All > can be attacked, and defended. :) > > All I want is the below (less code; and no dealing with unrelated things just > because I want to add hooks to a readonly class). > > ```php > > // I have a nice readonly class > final readonly class Entry > > { > public function __construct( > public string $word, > public string $slug, > ) {} > } > > // I simply want to add a hooked-property to that readonly class > final readonly class Entry > > { > public function __construct( > public string $word, > public string $slug, > public array $terms { > set(array $value) => array_map(static function (Term|array > $term): Term { > return $term instanceof Term ? $term : new Term(...$term); > }, $value); > get => $this->terms; // something, something > }, > ) {} > } > Your example might not need a "get" hook at all, if I understand correctly? The default behavior would make sense here (and, it seems for at least some part of the mailing list, is the *only* thing that makes sense). You would need a "set" hook, but I don't think anyone is objecting to that. If you'd like to propose "set" hooks in isolation, it seems like it would pass. > And for that, I believe, I provided a solution that is easy to reason about > (all details in previous mails), and allows everyone to achieve what they > want. I appreciate that you are focused on solving a real-world problem, and that you have implemented a solution that addresses the problem. I can understand that it seems like it's in reach, and that this conversation might feel like we're wasting time. That said, I think a number of people have reservations with the semantics of get hooks with readonly, and it's important that we take the time to ensure this feature is one that makes sense to all developers, and continues to push the language in the direction of being more consistent and hard to misuse. In an earlier email, Larry said, "`readonly` has always been misnamed; it should really be `writeonce`, because that's all it is. (Once again, this is likely the most poorly designed feature we've added in many years.)". I hope that I'm not misconstruing what he meant, but this makes it seem especially prudent to avoid making additional mistakes with regard to readonly.