Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:75459 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 79190 invoked from network); 14 Jul 2014 13:41:03 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 14 Jul 2014 13:41:03 -0000 Authentication-Results: pb1.pair.com smtp.mail=arvids.godjuks@gmail.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=arvids.godjuks@gmail.com; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.216.178 as permitted sender) X-PHP-List-Original-Sender: arvids.godjuks@gmail.com X-Host-Fingerprint: 209.85.216.178 mail-qc0-f178.google.com Received: from [209.85.216.178] ([209.85.216.178:47586] helo=mail-qc0-f178.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 0A/27-43645-DEDD3C35 for ; Mon, 14 Jul 2014 09:41:02 -0400 Received: by mail-qc0-f178.google.com with SMTP id x3so1543185qcv.37 for ; Mon, 14 Jul 2014 06:40:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc:content-type; bh=ywzX7yDA07ElwlDBsrgOzu8GP0V2Hb3ATObXrczgxgo=; b=P4O/0mfZCszQrxUgKAJ6B2v4fBtzSC5uzlPQqVUNH2NPFgb+OI8wzdbKIykaMjHWCr FTOe3c40L3BhAtD6Qeab6TV59bwhycWVK5PlbC4J6QGV7kXxYRThau6l8l+QBvRFu2ir X7N+2N3wQ9rGlzI38CQEbfeJR63TkTo/7AKLbZTn+TfUCiuO0ufcC8SVjVEcwwFYyg1A BD+b4xFelMTV75aSyIjKWgytTZlV8E71yab9PXm/JUgPOi2QeDFY4nrP83ml88AhkofG z/iFflaVgxYZdxB6sy40vEhm5Aa16TI5UJCvTJodLB+xo/czUpZWw4NqWW64JM0KFbG9 wl5w== X-Received: by 10.140.21.9 with SMTP id 9mr22897465qgk.39.1405345258294; Mon, 14 Jul 2014 06:40:58 -0700 (PDT) MIME-Version: 1.0 Received: by 10.140.30.137 with HTTP; Mon, 14 Jul 2014 06:40:38 -0700 (PDT) In-Reply-To: <53C3CFE6.90604@gmail.com> References: <08503591-EFC8-48E6-984E-FFC292C5EA5F@ajf.me> <027E65EF-C4FC-474C-92BB-D99EFADDEEED@ajf.me> <53C29EE4.3090808@gmail.com> <63e5b1c38d6e0c5111987e78d9b59be3@mail.gmail.com> <53C2AF35.40003@gmail.com> <93bb0c689ccca03a0af46c51abf0e11f@mail.gmail.com> <53C2C84A.6060308@garfieldtech.com> <49C63232-AB78-4B2C-A3E4-B21748D5D489@ajf.me> <53C2DC03.2050609@gmail.com> <53C3CFE6.90604@gmail.com> Date: Mon, 14 Jul 2014 16:40:38 +0300 Message-ID: To: Rowan Collins Cc: PHP internals Content-Type: multipart/alternative; boundary=001a11c1566ae8575b04fe2770ea Subject: Re: [PHP-DEV] [RFC] Scalar Type Hinting With Casts (re-opening) From: arvids.godjuks@gmail.com (Arvids Godjuks) --001a11c1566ae8575b04fe2770ea Content-Type: text/plain; charset=UTF-8 2014-07-14 15:41 GMT+03:00 Rowan Collins : > Arvids Godjuks wrote (on 14/07/2014): > > We already have quite good implementation for the array and object hints, >> they do their job and do it good. No need to change those at all - leave >> them as is - no need to add magic with casts and all that. Function/method >> requires an array? Make casts explicitly and pass it an array and not an >> integer. >> > > Yes, I totally agree. What I don't think makes sense is for the keyword > "array" in a function signature to mean "error if a string is passed", but > the keyword "int" in exactly the same place to mean "happily accept any > string (or even an array) and cast it to int, even if it's a lossy > conversion". > > If there is to be a short-hand for casts in function signatures, it should > not reuse the current syntax for type assertions, as they are not > equivalent facilities. > > > But this automatic >> casting should work only between string, integer, float, bool and null >> types and only between these. No automatic casting of these 5 to arrays or >> objects. >> > > Rather than special-casing this for "static types" (despite the fact that > arrays *can* be cast from other types) I would prefer to see it only happen > where it can be *lossless* (which is never the case for arrays, thus making > the current logic consistent with the new rule). This is essentially what > the current RFC proposes. > > I don't think that's actually overthinking it any more than the scalar vs > non-scalar version would, as it's a pretty easy rule to express. Broadly, > if (source_type)(target_type)$var == $var, that's a lossless cast. (There > are a couple of odd exceptions with very specific values, such as > (int)(array)1 == 1, but it's not too much of a stretch to say "all array > casts are considered lossy".) There is no special casing, Array and Object type hints are already implemented and are in the language for quite a long time. Just don't touch them, that's all I ask. Implement scalar hints and because of the nature of PHP - they need to have type casting built in. Otherwise you are forced to typecast on your own everything that comes from the outside sources: GET/POST/Databases/API's/REST/the whole lot. If it was a correct string that was converted to an int/float without any data loss - keep calm and carry on. --001a11c1566ae8575b04fe2770ea--