Remix.run Logo
phplovesong 2 hours ago

> And would be a massive headache to implement

Exactly. The type system was never built for anything even slightly more complex. Its basically annotations for primitive types and classes. PHP has always had an weak type system, so adding generics will most likely never happen.

> Adding generics to PHP would make CS fundamentalists somewhat happy

PHP has really only one collection datatype (the infamous array), so having generics would be tremendously useful, as an example you cant return an typed array from a function, witch is just really bad.

For an counter example, Python managed to do this, while also being a dynamic language, although having a stronger typing than PHP.

duckerude 3 minutes ago | parent | next [-]

Python managed to do this by not actually checking the types at runtime. If you declare a list[int] return type but you return a list[string] then nothing happens, you're expected to prevent that by running an offline typechecker.

PHP chose to check types at runtime. To check that a value is really an array<int> the runtime could have to loop through the entire array. All the types PHP currently implements are simple and cheap to check. For more elaborate cases you need an offline checker like PHPstan and comment-based type annotations. (PHPstan catches 99% of issues before the runtime gets to it so for my own code I'd prefer the Python approach with its cleaner syntax.)

The runtime checking seems the key difference, not so much the historical strength of the type system.

deaddodo 16 minutes ago | parent | prev [-]

> For an counter example, Python managed to do this, while also being a dynamic language, although having a stronger typing than PHP.

Exactly. Python is a dynamic strongly-typed language, PHP is a dynamic weakly-typed language.

Python's VM had the capability to add generics already built into the type system, PHP doesn't.

Ergo:

> and everything to do with not making sense to the type system

Strongly-typed languages (C++, Python, TypeScript) generally rely on generics (and other intrinsic features) to deal with functionality; weakly-typed ones (PHP, JavaScript, etc) tend to rely on reflection.