Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:102845 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 82210 invoked from network); 16 Jul 2018 11:44:40 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 16 Jul 2018 11:44:40 -0000 Authentication-Results: pb1.pair.com smtp.mail=mitke013@gmail.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=mitke013@gmail.com; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.218.51 as permitted sender) X-PHP-List-Original-Sender: mitke013@gmail.com X-Host-Fingerprint: 209.85.218.51 mail-oi0-f51.google.com Received: from [209.85.218.51] ([209.85.218.51:39262] helo=mail-oi0-f51.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id C9/A9-39793-6258C4B5 for ; Mon, 16 Jul 2018 07:44:38 -0400 Received: by mail-oi0-f51.google.com with SMTP id d189-v6so74122127oib.6 for ; Mon, 16 Jul 2018 04:44:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=dhVdmnygjabN09oXxYIP37H4809Ya6Ku7LnKsfpE+5c=; b=lSQjYmQUwYNpWg1i3Z8VU6s+wCx+ex5AOW0v89QonJYVXAiS2S3gcj13tTNndMW8F0 P5VZpCkSJj/DOHbAwNfjQqPzgbVbt2YhTS3eR4VsWji17mcL8ui5uQrd6rN7YDPI6wJl Of7lmcbrsmkoUEUB14vcTJvTd/ttMavkS5eAScmBnLlwmLYuBLvdYna6J0AOB1wS76cO xN6nXd+UflelpkhhH8S4o9zGmGhUjTXkw0c63fdRqsWVAtAyOYwh7uTQEy0j8JWjdLqQ 1iGgNurfQ7ab481cpEFhZtst8liZUO3RddxhSRTT+gT7HYS/A3AkXL/fEK2/7zEjWjTA Kwsw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=dhVdmnygjabN09oXxYIP37H4809Ya6Ku7LnKsfpE+5c=; b=lAB7dJrBTkMXK+ih/VQwG2L652xXbtUYOs86Z4Q1r7g0boqpDfzKTtFN5qaqtpuXFG LdNINA9/JC8s6mVZTgJQ8EdZzlY2nTmhtgaikB0HsVdUaQ8KpDEii3N2mj8ZvK6Az66T FheQTVTIg5Pg28EDfLaeXds6lomGYOPhbk8VS+FyNy2KReFOeka+qqi/SOMjGSyu0SgD JqbXSX5cnaRkalQAZQCpn3eY2e7FrjGQogJN8HqRTdyhClRPjhD/4cnvTOr4GZ8chdRB mPu5u8G+sI3p79kPSO0P1TdcM2id+7r2YRI63n9/y1iF5b2tjhcB3UEt6TxuUT0/hDnF BAdg== X-Gm-Message-State: AOUpUlEl69abm/sHTkRGfhp7dscGWdfYp4n+xqtew05ajQoL0L6OeFnI oKnzAkdF/V7CytnIpz4Gq6SBLaqr6Q4sMRJyr1y6fQ== X-Google-Smtp-Source: AAOMgpcnenNkuIDZfDWyaKLzMi5OTjmjk5Wvuccm3dce/T8pGQ1QmB/gnWrmvwObXtv9S+03FciQ934f3m4wnTtNTxo= X-Received: by 2002:aca:69d0:: with SMTP id e199-v6mr16894560oic.286.1531741476150; Mon, 16 Jul 2018 04:44:36 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: Date: Mon, 16 Jul 2018 13:44:24 +0200 Message-ID: To: nikita.ppv@gmail.com Cc: internals@lists.php.net Content-Type: multipart/alternative; boundary="00000000000092e1de05711c5ad3" Subject: Re: [PHP-DEV] [RFC] Optional typehint check for parameters From: mitke013@gmail.com (Zeljko Mitic) --00000000000092e1de05711c5ad3 Content-Type: text/plain; charset="UTF-8" On Mon, Jul 16, 2018 at 1:14 PM Nikita Popov wrote: > On Sun, Jul 15, 2018 at 9:39 PM, Zeljko Mitic 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. --00000000000092e1de05711c5ad3--