Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:75443 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 53398 invoked from network); 14 Jul 2014 12:43:13 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 14 Jul 2014 12:43:13 -0000 Authentication-Results: pb1.pair.com header.from=rowan.collins@gmail.com; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=rowan.collins@gmail.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 74.125.82.169 as permitted sender) X-PHP-List-Original-Sender: rowan.collins@gmail.com X-Host-Fingerprint: 74.125.82.169 mail-we0-f169.google.com Received: from [74.125.82.169] ([74.125.82.169:43310] helo=mail-we0-f169.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id E1/81-43645-F50D3C35 for ; Mon, 14 Jul 2014 08:43:13 -0400 Received: by mail-we0-f169.google.com with SMTP id t60so4064356wes.0 for ; Mon, 14 Jul 2014 05:43:08 -0700 (PDT) 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=kQSO8FO6qgJLDFYQlUAmwtUxb04nAgSvJje10znZ0K4=; b=Pu9ZZLVTgVxhelbd9jdTDzN9suRdrHM6CpVR5YTXTnRP0mO1R8yCeRdDxfiJ4HvSA2 r9vdxiIm7HgyxeAIjWpqt1xrCswmHD7Np/sqFaGvFKhWX/QRqIBkbxfrzZ6bkR1+LDp7 HC9l1TltMLuXddYM38kCQddWqpLMKCvTXGRJXLIEIzeO4N74GGAOcEgXIw+9kWmKdsw/ kWcPAPhyx2AQCfcP1QyTYhEQw+naNsar+pRxRcwu8+2LEz+i5OcQ4GB27Lq7g4xXOyQR +BeiR59sc2pc/zAITR8g0XTzYtnzOrlhSeCd+6vfqG83OnfgrFMIriPhdt66C911mSqU g4cw== X-Received: by 10.194.59.65 with SMTP id x1mr18775975wjq.60.1405341787709; Mon, 14 Jul 2014 05:43:07 -0700 (PDT) Received: from [192.168.0.139] ([62.189.198.114]) by mx.google.com with ESMTPSA id pq9sm25280335wjc.35.2014.07.14.05.43.06 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Mon, 14 Jul 2014 05:43:06 -0700 (PDT) Message-ID: <53C3CFE6.90604@gmail.com> Date: Mon, 14 Jul 2014 13:41:10 +0100 User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:24.0) Gecko/20100101 Thunderbird/24.6.0 MIME-Version: 1.0 To: internals@lists.php.net 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> In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Subject: Re: [PHP-DEV] [RFC] Scalar Type Hinting With Casts (re-opening) From: rowan.collins@gmail.com (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".) > You just need to get a warning, that you can disable via > error_reporting, and just make your prototype with all the type hints you > need and deal with the cast warnings in the type hints later on. As I've said elsewhere, I see the usefulness of emitting warnings of some kind for "lossy" casts as a separate question from the usefulness of performing such casts inline in function signatures. It makes sense for the language to implement both at once, but for the user to be able to use them separately (e.g. catch lossy conversion in existing uses of (int)$foo). Regards, -- Rowan Collins [IMSoP]