Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:81664 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 76002 invoked from network); 2 Feb 2015 22:38:30 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 2 Feb 2015 22:38:30 -0000 Authentication-Results: pb1.pair.com header.from=cmbecker69@gmx.de; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=cmbecker69@gmx.de; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmx.de designates 212.227.17.20 as permitted sender) X-PHP-List-Original-Sender: cmbecker69@gmx.de X-Host-Fingerprint: 212.227.17.20 mout.gmx.net Received: from [212.227.17.20] ([212.227.17.20:56318] helo=mout.gmx.net) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 11/7A-25089-36CFFC45 for ; Mon, 02 Feb 2015 17:38:28 -0500 Received: from [192.168.0.100] ([91.67.244.80]) by mail.gmx.com (mrgmx102) with ESMTPSA (Nemesis) id 0LlGoc-1XhQSJ2Q0k-00b4W1; Mon, 02 Feb 2015 23:38:21 +0100 Message-ID: <54CFFC5D.4000802@gmx.de> Date: Mon, 02 Feb 2015 23:38:21 +0100 User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:31.0) Gecko/20100101 Thunderbird/31.4.0 MIME-Version: 1.0 To: Thomas Bley , dmitry@zend.com CC: ajf@ajf.me, nikita.ppv@gmail.com, internals@lists.php.net References: <20150202210349.6FB91261948@dd15934.kasserver.com><54CFE965.2080905@gmx.de> <20150202214254.BEB88261948@dd15934.kasserver.com> In-Reply-To: <20150202214254.BEB88261948@dd15934.kasserver.com> Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit X-Provags-ID: V03:K0:ElbIMgQD/eKEheRhGaBBBDIxVreWUwKoas0zC+ADXdUnPsB8r2n uPeKXkuxBtKrhyddemCIiXxTPpDMOWqYTa5NOHzTMvnszicdtA6FWmkiJ//QorONzjkKFP0 iJ3/CTC25n8VN/wQZsC2e51Wn/FZfv2ZHsgVistMi5lhmT93gmnE8hrWY7X5crV/cjejVU7 mtfqgWT/+opJ1uabyTYag== X-UI-Out-Filterresults: notjunk:1; Subject: Re: [PHP-DEV] What do we need strict scalar type hints for? From: cmbecker69@gmx.de (Christoph Becker) Thomas Bley wrote: > Yes, we currently have negative values for invoices and positive for vouchers ... > > > Christoph Becker wrote on 02.02.2015 22:17: > >> Thomas Bley wrote: >> >>> Here is a typical billing example which uses exceptions: >>> >>> >> ini_set('error_reporting', E_ALL); >>> ini_set('display_errors', 1); >>> >>> addVat('apples'); >>> >>> function addVat($amount) { >>> if (!is_int($amount) && !is_float($amount)) { >>> throw new InvalidArgumentException('Argument 1 passed to '.__FUNCTION__.' >>> must be of the type int|float, '.gettype($amount).' given'); >>> } >>> return round($amount*1.19, 2); >>> } >>> >>> Instead of multiple strict scalar hints (e.g. function addVat(int|float >>> $amount){...}), I would prefer to have "numeric" as strict type: >>> function addVat(numeric $amount){...} >> >> Have you considered >> >> addVat(-1); Well, my point was that even a strict type system doesn't necessarilly catch all erroneous/undesired arguments. Even if addVat() properly handles negative numbers, and maybe even zeroes, there are functions that can't. IMHO strict typing in a dynamic language is overvalued. While it certainly helps to catch some errors, it doesn't do so during compile time, at least not for all cases (consider call_user_func() etc.)[1] And frankly, I don't see what's wrong with a weak type hint in this case: function addVat(float $amount) {...} You're returning a float, anyway. [1] There may be other benefits of having strict type hints. One of these would be potential optimizations, but I have some doubts whether this is feasible with optional weak/strict type hinting on the *call* side. OTOH, strict type hints would require a lot of manual conversions. -- Christoph M. Becker