On Mon, Jul 16, 2018 at 1:44 PM Zeljko Mitic <mitke...@gmail.com> wrote:
> > > On Mon, Jul 16, 2018 at 1:14 PM Nikita Popov <nikita....@gmail.com> wrote: > >> On Sun, Jul 15, 2018 at 9:39 PM, Zeljko Mitic <mitke...@gmail.com> wrote: >> >>> PHP is dynamic language and each typed typehinted parameter has to be >>> checked every time. I am suggesting new php.ini value "typecheck.enable = >>> 1" which can be turned off. >>> >>> Example: >>> with default php config, a code like this would check each member of >>> $users >>> array: >>> >>> function demo(User ...$users) {} >>> >>> but with "typecheck.enable = 0", same code would be treated like it was >>> written this way: >>> >>> function demo(...$users) {} >>> >>> Basically, php would simply *ignore* typehints and work like they are not >>> there. >>> >>> It would be programmers responsibility to send correct values but people >>> using typehints are not those still learning the basics. >>> >>> Just like it is programmers responsibility to restart php-fpm when using >>> opcache.validate=0, it would be theirs responsibility to send correct >>> values. Any decent IDE would mark errors for wrong parameter type and >>> with >>> tools like phpstan, it is almost impossible to make one. Only development >>> computers would need to have this checks turned on. >>> >>> This creates no BC problems as it is a new feature. Given that typechecks >>> can be turned off, all existing programs would benefit from extra speed. >>> >>> I am sending this mail by following rules from >>> https://wiki.php.net/rfc/howto . I don't have any knowledge of php >>> internals so unfortunatelly, I can't help with this. I at least hope >>> someone will take this RFC into consideration. >>> >> >> Independently of the question of whether we want to do this at all or >> not, there is a technical difficulty here: While most type checks are just >> that, scalar type checks (in particular in coercive mode) may perform type >> conversions. You pass "42" to a parameter accepting int and the parameter >> will become 42. >> >> Just disabling type checks completely would mean that the function now >> receives "42", which actively violates the type contract (even though the >> code was completely error free with enabled type checks). >> >> How do you propose to handle these cases? >> >> Nikita >> > > That is good question, I didn't think of that. I think better solution > would be to only typecheck objects, conversion still happens. > > I didn't want to mention the story behind it. I would really like php to > have generics, or at least typed arrays. I recently read that someone tried > to implement typed array and got 15% slow-down; I am sorry I can't provide > link, reddit is just to big place :) > > So if speed is major problem of advancing towards generics/typed arrays, I > think this optional feature would solve it. Programmer would still spot > problem on dev computers where speed is not relevant. > > I hope I explained this correctly, english is not my first language. > I am really sorry for messing up my previous message. First, I clicked "reply" instead of "reply all". When I saw that, I used copy&paste and my last message appeared as quote. I hope you can read it, this will (hopefully) not happen again.