Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:86048 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 51108 invoked from network); 30 Apr 2015 00:22:25 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 30 Apr 2015 00:22:25 -0000 Authentication-Results: pb1.pair.com smtp.mail=yohgaki@gmail.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=yohgaki@gmail.com; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.214.170 as permitted sender) X-PHP-List-Original-Sender: yohgaki@gmail.com X-Host-Fingerprint: 209.85.214.170 mail-ob0-f170.google.com Received: from [209.85.214.170] ([209.85.214.170:36251] helo=mail-ob0-f170.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 38/74-29050-0C571455 for ; Wed, 29 Apr 2015 20:22:24 -0400 Received: by obbeb7 with SMTP id eb7so32889704obb.3 for ; Wed, 29 Apr 2015 17:22:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:sender:in-reply-to:references:from:date:message-id :subject:to:cc:content-type; bh=xQx5XsmZL17sQrx5JBloVFXfTputnTZTU5S3u5aGH8M=; b=cnJeBFaaQMWHlztSDWj5zyNvcYis66D5bgCzG02PBSE0JrL3mNQhTiMaSt/j3Hsuz/ u2x1EfmrKZxdhFq6wJCjD7+ZUJ0Qj2nqA+74aRGvSCHxyl13EbtlpLJ3Oa96BUotynyU ti9F3uvNutjebnYmjaokPcOCtCkqAqq8cCAXcnM6WTWIsRceP4YtPeXVSW6dRfMvvyLL ARVeqKsVgauN0xnPRit+q5t0aMr7qoHillJEnnmmiPSK9tAm536G7WSC9L4HXD5MZe22 oQoZY7ZGF9hxSKvfkYcTKp3kJuubhXKR7IXGukOyIs1T8dKN3DCErnkm9uWOPb/+2jhX JRyQ== X-Received: by 10.60.65.97 with SMTP id w1mr1392143oes.10.1430353341812; Wed, 29 Apr 2015 17:22:21 -0700 (PDT) MIME-Version: 1.0 Sender: yohgaki@gmail.com Received: by 10.202.104.196 with HTTP; Wed, 29 Apr 2015 17:21:40 -0700 (PDT) In-Reply-To: References: <55401F31.9030703@gmail.com> <55414CC5.7000503@gmx.de> Date: Thu, 30 Apr 2015 09:21:40 +0900 X-Google-Sender-Auth: z1vZ8n8pmi_MOzL79wLpIb0EyoE Message-ID: To: Nathan Bruer Cc: Christoph Becker , Ryan Pallas , Stanislav Malyshev , "internals@lists.php.net" Content-Type: multipart/alternative; boundary=001a11c2558ed796970514e616a5 Subject: Re: [PHP-DEV] Adding "numeric" type hint From: yohgaki@ohgaki.net (Yasuo Ohgaki) --001a11c2558ed796970514e616a5 Content-Type: text/plain; charset=UTF-8 Hi Nathan, On Thu, Apr 30, 2015 at 6:41 AM, Nathan Bruer wrote: > This has been a big issue that I have ran into many times in the past for > large framework projects, instead of building it out with "strict" types > like: int, float, string, exc... It makes more sense to allow a user to > define a psudo-type themselves which PHP will pass the arguments into to be > "cleaned" by the user then let the function deal with the arguments. > > For example: > function force_int($value){ > return (int) $value; > } > function must_be_string($v){ > if(!is_string($v)){ > throw new Exception("Not String"); > } > return (string) $v; > } > > function foo(*force_int $val, *must_be_string $str){ > // $val will always be an int if it ever gets here > // $str will always be a string if it gets here > echo $val, " ", $str; > } > foo("foo", "bar"); // returns '0 bar' > foo("1", "2"); // returns '1 2' > foo(1, 2); // fatal error because second argument threw exception > foo(1, "2") // returns '1 2' > foo("hi", 2) // fatal error because second argument threw exception > foo("hi", "2") // returns '0 2'; > I agree. Forcing type is headache and such check is useful. The problem with this approach is overhead. I would like to resolve this in 2 ways. 1. DbC - Check parameter types during development by DbC and make sure all callee satisfies contract. 2. True weak type hint - Make weak mode type hint truly weak. Accept any form of the specified types. Since we won't have DbC for PHP 7.0, I'll discuss only option 2. function foo(int $i) { switch(gettype($i)) { case 'integer': case 'float": foo_numeric($i); break; default: // We don't want to accept gmp/string throw new Exception('Type error'); } } Ideal solution would be DbC. Anyway, above example is a little more efficient than calling pseudo type checker functions (for now). Pseudo type checker is useful and requires less code. I'm neutral to have it or not. Regards, -- Yasuo Ohgaki yohgaki@ohgaki.net --001a11c2558ed796970514e616a5--