On Thu, 25 Jul 2019 at 14:48, Nikita Popov <nikita....@gmail.com> wrote:
> I think nowadays it is well known that by-reference passing is to be > avoided and I don't see it particularly commonly in user code. By-reference > passing is mainly used when it is needed to interact with existing > by-reference functions such as preg_match(). We can hardly switch these > functions to use out/inout if we require the corresponding keyword on the > call-site. > I guess the call-site syntax would still need to be opt-in for compatibility reasons, but we could definitely mark the parameters as "out" in internal functions, even if that was mainly a documentation / reflection change. That would stop people having to write `$matches = []; preg_match($foo, $bar, $matches);` to ensure that the output parameter is initialised. I have been annoyed by that more often than I've encountered a function where I wasn't sure if the parameter was by-reference or not. This proposal (in conjunction with the option to make it required) would > solve the main issues I have with the by-reference passing implementation > If this remains optional, I wouldn't have much appetite for using it, because the benefit feels very slight. The fact that it wouldn't always be mandatory makes the benefit even slighter, since you still couldn't look at foo($bar) and know whether it was by-reference without also knowing what declare options were in scope. > the out/inout approach is a refinement over that, but I'm not convinced > that it a worthwhile refinement relative to the language and engine > complexity it will introduce. > Would it really be that complex? The only real difference between "out" and "&" would be automatically setting the variable to null when it was passed to the function. Regards, -- Rowan Collins [IMSoP]