Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:81737 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 60146 invoked from network); 3 Feb 2015 20:23:42 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 3 Feb 2015 20:23:42 -0000 Authentication-Results: pb1.pair.com smtp.mail=mails@thomasbley.de; spf=permerror; sender-id=unknown Authentication-Results: pb1.pair.com header.from=mails@thomasbley.de; sender-id=unknown Received-SPF: error (pb1.pair.com: domain thomasbley.de from 85.13.137.24 cause and error) X-PHP-List-Original-Sender: mails@thomasbley.de X-Host-Fingerprint: 85.13.137.24 dd15934.kasserver.com Received: from [85.13.137.24] ([85.13.137.24:48270] helo=dd15934.kasserver.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id B0/58-20608-C4E21D45 for ; Tue, 03 Feb 2015 15:23:41 -0500 Received: from dd15934.kasserver.com (dd0803.kasserver.com [85.13.146.34]) by dd15934.kasserver.com (Postfix) with ESMTPSA id D5D5626193D; Tue, 3 Feb 2015 21:23:36 +0100 (CET) MIME-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 8bit X-SenderIP: 178.19.222.74 User-Agent: ALL-INKL Webmail 2.11 In-Reply-To: <20150203200757.6b29a758@main> References: <20150202210349.6FB91261948@dd15934.kasserver.com> <54CFE965.2080905@gmx.de> <20150202214254.BEB88261948@dd15934.kasserver.com> <54CFFC5D.4000802@gmx.de><20150203200757.6b29a758@main> To: cmbecker69@gmx.de, sd@sven-drieling.de Cc: dmitry@zend.com, ajf@ajf.me, nikita.ppv@gmail.com, internals@lists.php.net Message-ID: <20150203202336.D5D5626193D@dd15934.kasserver.com> Date: Tue, 3 Feb 2015 21:23:36 +0100 (CET) Subject: Re: [PHP-DEV] What do we need strict scalar type hints for? From: mails@thomasbley.de ("Thomas Bley") I would prefer: function addVat($amount) { validateAmount($amount); return round($amount*1.19, 2); } function validateAmount($amount) { if (!is_int($amount) && !is_float($amount)) { throw new InvalidArgumentException('Argument amount must be of the type int|float, '.gettype($amount).' given'); } } Regards Thomas Sven Drieling wrote on 03.02.2015 20:07: > Am Mon, 02 Feb 2015 23:38:21 +0100 > schrieb Christoph Becker : > > Hallo, > >> >> 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. > > What about scalar type declaration in userland? > > > namespace mytypes; > > > declare scalartype amount($amount) { > if (!is_int($amount) && !is_float($amount)) { > throw new InvalidArgumentException('Argument amount > must be of the type int|float, '.gettype($amount).' given'); > } > } > > > function addVat(mytypes\amount $amount) { > return round($amount*1.19, 2); > } > > addVat(42) // OK > addVat("42") // OK > addVat(-42) // OK > addVat(42.0) // OK > addVat(true) // Exception > > > var mytypes\amount $amount = 0; > > $amount = 42; // OK > $amount = "42"; // OK > $amount = true; // Exception > > > > tschuess > [|8:) >