3 ms·
> There are no generics, and from what I understand for some odd reason will never be even though Hack forked from PHP and managed to add them? The reason is p
by donatj 3y ago
> There are no generics, and from what I understand for some odd reason will never be even though Hack forked from PHP and managed to add them?
The reason is pretty simple. Hack has a compilation step. PHP does not. Generics in an interpreted language means runtime errors, which are far less valuable and more troubling than compilation errors.
PHP’s loose typing + an offline static analyzer like phpstan will get you roughly the same result without the runtime errors.
- hu3 3y agoWhat a coincidence. There's a video about generics in PHP, posted 2 hours ago: https://youtu.be/JtmRG5lCENA https://youtu.be/JtmRG5lCENA
- Ciantic 3y agoPHP doesn't have loose typing anymore, if you make a function parameter with a specific type: function test(bool $param) {} You will get an runtime error if you call it with wrong type: Fatal error: Uncaught TypeError: test(): Argument #1 ($param) must be of type boolean However generics are probably costly to typecheck runtime? I haven't studied the reason why there is not generics planned.
- WesolyKubeczek 3y agoIn a dynamic interpreted language there is no value of having generics over duck typing or omitting types altogether (and you can narrow down by specifying interfaces). Compiled languages generate a version of your function<T> for every T encountered in the codebase.
- usrusr 3y agoFor completeness: other compiled languages elide that T and use the types in angulars only to emit errors during compilation. Also perfectly fine, but certainly not any help in absence of static compilation.