Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:80646 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 19173 invoked from network); 16 Jan 2015 15:59:36 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 16 Jan 2015 15:59:36 -0000 Authentication-Results: pb1.pair.com smtp.mail=rowan.collins@gmail.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=rowan.collins@gmail.com; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 74.125.82.171 as permitted sender) X-PHP-List-Original-Sender: rowan.collins@gmail.com X-Host-Fingerprint: 74.125.82.171 mail-we0-f171.google.com Received: from [74.125.82.171] ([74.125.82.171:62294] helo=mail-we0-f171.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 31/03-00273-76539B45 for ; Fri, 16 Jan 2015 10:59:36 -0500 Received: by mail-we0-f171.google.com with SMTP id u56so21005420wes.2 for ; Fri, 16 Jan 2015 07:59:32 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=message-id:date:from:user-agent:mime-version:to:subject:references :in-reply-to:content-type:content-transfer-encoding; bh=KHLt0QryzrsRZpnT1pLBmQC9PZ3GJTLtTlDTKsRIJG8=; b=bM1gggTRa+oOihM3mLugHMRn9qKcyTiYPfDa0UDDgqYigW4XB60ddCaRgl/zxoZO7/ T7hLv3eWNaSVJtWAzVXaINb5Yb+YvtsoVksfyj3/ZZ26FnKYlMa+BPuKUykAeobM4PS2 IMnTsY2Kn97NWqme7Xd/k5JTZTcodMs/eOSclzm73EXPfVBk/II0UFCM+aG4MfTaX1jU Qwyd07249N2+hRicHWnVCi4l6mnJ4OkHb9nL3cvosuJAEp7mlnT0V3iVOyNJULudyWJT 7piw/xmWoYrGF/m+61vuy3X/XdooRz7w5C1ZqCK5PlEQ+YZcWScjY3g+GOwspJNhtP8j w+9w== X-Received: by 10.180.87.129 with SMTP id ay1mr7695255wib.60.1421423967687; Fri, 16 Jan 2015 07:59:27 -0800 (PST) Received: from [192.168.0.172] ([62.189.198.114]) by mx.google.com with ESMTPSA id a14sm3480360wib.22.2015.01.16.07.59.26 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 16 Jan 2015 07:59:26 -0800 (PST) Message-ID: <54B93542.8060105@gmail.com> Date: Fri, 16 Jan 2015 15:58:58 +0000 User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:31.0) Gecko/20100101 Thunderbird/31.4.0 MIME-Version: 1.0 To: "internals@lists.php.net" References: <8DCD1B72-C81D-499E-B455-E4A042CD76E6@ajf.me> <4E2073DE-0951-498C-97BB-DDAC094F11FA@ajf.me> <9a033dd1f223f854e760924d118ab812@mail.gmail.com> <2ae0164cb9b9bf1c974d7a3c60af0466@mail.gmail.com> <6105ea99002e634373c09685310e26a6@mail.gmail.com> <54B90431.10903@gmail.com> <54B925B8.1010601@gmail.com> In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit Subject: Re: [PHP-DEV] [RFC] Scalar Type Hints v0.2 From: rowan.collins@gmail.com (Rowan Collins) Andrey Andreev wrote on 16/01/2015 15:37: > With no attempt to argue, I just thought of another example, inspired > by the ones given in the RFC - it is fairly common for functions > dealing with times to only accept a UNIX timestamp or otherwise just a > single kind of a time unit, such as only a count of minutes or days. > > So where '7 years' is passed and and accepted when using a weak hint > (by truncating the trailing non-numeric characters), a strict hint on > the other hand may prevent the input of incorrect data (given that > it's not years that are expected, of course). That's a nice example of the advantage, actually, thank you. However, it makes me think of another approach to the whole situation, which is to make more use of Value Objects (now that the implementation of objects is no longer a huge performance hog). Obviously, for a timestamp, there is the built-in DateTimeInterface, but even if there weren't, it would be trivial to create something which existed only for type-checking a kind of sub-classed integer: class UnixTimestamp { private $value; public function get_value() { return $this->value } public function __construct($new_value) { /* validate input */ } } Obviously, the validation would still need to be written, but it would be tucked away in the shared class, rather than pasted as boilerplate at the top of every function. In many situations, the type of the argument isn't really "int" or "string", it's "mode" (from an enum or bitmask), or "text label" (maybe needs translation support) etc. So a library that really wants to document its type system should be providing wrappers for all these value types, at which point it can type hint them all by class or interface anyway. Scalar type hinting falls into the in-between category of "I want my code to be type-safe, but using a very flat type system which makes only loose guarantees about the data". Regards, -- Rowan Collins [IMSoP]