Den 2016-03-22 kl. 14:15, skrev Rowan Collins:
Nikita Popov wrote on 21/03/2016 17:05:
While it is no secret that we don't particularly like references,
this is not the reason why they are forbidden. There is no conspiracy
to punish users of references by denying them use of new typing
features. No, the reasons here are technical. Let me illustrate some
of the issues involved in more detail.
Thanks for the detailed explanation of the problems, which are indeed
deeper than I'd appreciated. To be clear, I never thought it was the
intention to dissuade users from using references, but a couple of
people have implied that they consider it no bad thing.
For me, the restriction on setting references makes this whole feature
a no-go for the language as it stands. I don't think it is at all
reasonable for the following code to produce a TypeError:
class Sorter {
private array $data;
public function __construct(array $data) {
$this->data = $data;
sort($this->data);
}
}
new Sorter([1,3,6,5,2,4]);
It may be that we've reached the limit of how much typing we can add
to the language incrementally, and need actual typed variables, so
that references could be made only between variables with the same
type hint, e.g.
class A { public int $foo=42; }
$a = new A;
int $ref =& $a->foo;
The sort() example would work in this scenario because the expected
parameter type is array, so the reference being created is of the
matching type.
Obviously, this would be a huge change to the language, and no doubt
have all sorts of impacts and problems of its own, but I don't think
it makes sense to say "you can typehint your property names, but you
lose some existing functionality if you do so, for technical reasons".
Regards,
Hm... In Hack the above code works, see:
https://3v4l.org/nsA5W
Well they have a different implementation as mentioned in
the RFC, so no wonder. But they do have typed porperties in
the language.
Regards //Björn Larsson
--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php