Hi

On 4/21/23 12:49, Deleu wrote:
What is fundamentally different about Type Alias as opposed to something
like Enums that was recently introduced?

Type aliases might include an unbounded number of types and are … aliases, which based on [1] is a major issue.

To my understanding, PHP currently assumes that no object of a non-existent type can exist, thus is able to short-circuit checks like:

    $foo instanceof Bar; // If Bar does not exist,
                         // $foo cannot be Bar by definition.

However this might be violated if such type aliases exist, as Bar could be an alias to a type that actually exists. Bar could probably even be an alias to another alias:

    type Bar = Baz|Quux
    type Baz = Foo

So to check if `$foo instanceof Bar` in this case, the engine would need to load Bar, Baz, Quux and Foo in order to determine if $foo is any of these.

If folks also want intersection types in there, which they likely want, this gets complicated quickly, because nested type definitions would not necessarily be in DNF and thus expensive to check. Consider:

    type Foo = Bar&Baz;
    type Bar = Quux|Asdf;
    type Baz = Apple|Banana

Then `type Foo = (Quux|Asdf)&(Apple|Banana);` which is not in DNF and explodes into this type when written in DNF: `type Foo = (Quux&Apple)|(Quux&Banana)|(Asdf&Apple)|(Asdf&Banana)`.

So you'd likely have to eagerly load all pieces of the definition and normalize it, whereas to my understanding currently many type checks can happen more lazily. The normalization in itself can be expensive as shown with the combinatorial explosion above.

Disclaimer though: I'm just parroting what smarter people than me have said before and also drawing my own conclusions from my understanding of what they said.

Best regards
Tim Düsterhus

[1] https://chat.stackoverflow.com/transcript/message/55310273#55310273

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: https://www.php.net/unsub.php

Reply via email to