On 6 June 2018 at 07:14, Ryan Jentzsch <ryan.jentz...@gmail.com> wrote:

> Why would something like this not work?
>
> strict class MyClass
> {
>     protected int $foo = 1;
>     public string $bar = "strict keyword in front of class allows/enforces
> strict properties";
>     private string $isItReallyThatDifficult = "to implement this?";
> }
>


The problem is not in defining what code has type annotations, it's what to
do with them afterwards.

The performance hit comes from the fact that *every* time you run:

$foo->bar = $baz;

The engine now needs to check whether $foo is a class with type
constraints, and what the type constraint of bar is, and whether the type
of $baz matches that constraint. The answer will be "no constraint" most of
the time, but it doesn't know that until it checks, at run-time, every time.

Nor is it just straight-forward assignments that would need to change,
there are knock-on effects across the language, such as assignment by
reference:

strict class MyClass
{
    public int $foo;
    public string $bar;
}

$a = new MyClass;
$inst =& $a->foo; // If this is allowed...
$inst = 'bad'; // ...then this needs to error

$s = 'good';
$a->bar =& $s; // And if this is allowed...
$s = 42; // ... then this needs to error

The previous proposal for typed properties simply raised errors when you
tried to use reference assignment with any typed property, which feels like
an arbitrary restriction to me. The alternative is to allow *every variable
in the language* to carry type constraint information. I mused some more on
this here: http://rwec.co.uk/q/php-type-system

In short, yes, it really is that difficult.

Regards,
-- 
Rowan Collins
[IMSoP]

Reply via email to