Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:58400 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 1005 invoked from network); 1 Mar 2012 09:36:30 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 1 Mar 2012 09:36:30 -0000 Authentication-Results: pb1.pair.com header.from=linepogl@gmail.com; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=linepogl@gmail.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.161.170 as permitted sender) X-PHP-List-Original-Sender: linepogl@gmail.com X-Host-Fingerprint: 209.85.161.170 mail-gx0-f170.google.com Received: from [209.85.161.170] ([209.85.161.170:47745] helo=mail-gx0-f170.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id F7/3F-46815-D134F4F4 for ; Thu, 01 Mar 2012 04:36:30 -0500 Received: by ggki2 with SMTP id i2so122909ggk.29 for ; Thu, 01 Mar 2012 01:36:27 -0800 (PST) Received-SPF: pass (google.com: domain of linepogl@gmail.com designates 10.101.175.2 as permitted sender) client-ip=10.101.175.2; Authentication-Results: mr.google.com; spf=pass (google.com: domain of linepogl@gmail.com designates 10.101.175.2 as permitted sender) smtp.mail=linepogl@gmail.com; dkim=pass header.i=linepogl@gmail.com Received: from mr.google.com ([10.101.175.2]) by 10.101.175.2 with SMTP id c2mr1734521anp.73.1330594587473 (num_hops = 1); Thu, 01 Mar 2012 01:36:27 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc:content-type; bh=X/BUiaHlN4S0JkUsaJmHdPEVIQ45wPbFcDhuRjWBhvM=; b=YjnCvcGn9ZTMCoNlMQSVHZQMnl6ERtskGQoE3VmxcW5SOPklb3nOnGkQeS1bG2Y5A1 SdIEn5qcgxG/ox4Kh1NF1oV7IGz3i24owqtgoAOv23lhXkryFDnBK8Z+01xSnkF5QfCN iw6r9FM0VK9DAC0QRxhg81L7NxQqNgB/BLzBo= Received: by 10.101.175.2 with SMTP id c2mr1343173anp.73.1330594587389; Thu, 01 Mar 2012 01:36:27 -0800 (PST) MIME-Version: 1.0 Received: by 10.147.125.8 with HTTP; Thu, 1 Mar 2012 01:36:07 -0800 (PST) In-Reply-To: References: Date: Thu, 1 Mar 2012 10:36:07 +0100 Message-ID: To: Adam Jon Richardson Cc: internals@lists.php.net Content-Type: multipart/alternative; boundary=001636c9293cb8afad04ba2b31a2 Subject: Re: [PHP-DEV] Scalar Type Intentions From: linepogl@gmail.com (Lazare Inepologlou) --001636c9293cb8afad04ba2b31a2 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable > > And, *what if PHP added the following aliases for the hint scalar*: - bool - int - float - string > If an object has a __toString method, does it qualify as a valid value to be passed to a scalar argument? In my opinion, it should. Your suggestion has a future compatibility problem. The introduction of new type casting methods (like __toInt or like __castTo) is an open possibility. In such a case, if those keywords are nothing but aliases for "scalar", then there will be no way to choose which type casting method should be chosen. Lazare INEPOLOGLOU Ing=C3=A9nieur Logiciel 2012/3/1 Adam Jon Richardson > PHP currently allows users to provide type hints for functions and method= s, > although the type hints are currently limited to objects and arrays: > http://en.wikipedia.org/wiki/Type_system#Variable_levels_of_type_checking > > Restricting the type hinting to arrays and objects makes sense, as PHP's > type juggling (similar in many ways to JavaScript's), a core feature of t= he > language, performs automatic conversions between scalars as determined by > context: > http://php.net/manual/en/language.types.type-juggling.php > > However, the lack of scalar hinting does limit the ability of a developer > to declare his/her intentions for function/method parameters. A non-hinte= d > parameter expecting a scalar could be sent an object or an array, breakin= g > the expectations (and much of the time, the functionality) of the code. > That is to say, there is a value in declaring what the parameter IS NOT, > too. > > For example, the function below could have an object or array passed as t= he > first argument, even though the expectation is a scalar: > > function foo($arg){ > // $arg is supposed to be a scalar and will be used as an int > } > > *What if PHP added the hint scalar?* The example could be rewritten as: > > function foo(scalar $arg){ > // $arg is supposed to be a scalar and will be used as an int > } > > The idea is that the failure to send a valid scalar argument to the > function would result in the same feedback that currently occurs when arr= ay > or object hints are not heeded. Now, the expectations for each > function/method parameter can be explicitly declared (or, at least, more > explicitly.) > > And, *what if PHP added the following aliases for the hint scalar*: > > - bool > - int > - float > - string > > The function could then be rewritten as below: > > function foo(int $arg){ > // $arg is supposed to be a scalar and will be used as an int > } > > Now, the aliases wouldn't buy any more precision in terms of PHP's parser > expectations. Your function/method parameters can only expect objects, > arrays, or scalars. However, the aliases would allow developers to better > communicate intentions AND provide more information for IDE's and static > analyses tools. And, again, the use of the scalar hint and its aliases > would preclude sending objects and arrays to functions expecting otherwis= e. > > I realize this subject is heated, and people's patience has worn thin. I'= m > just hoping to toss out this idea I've had for some time while all of the > concerns on both sides are still fresh in my head. > > Thanks for reading :) > > Adam > --001636c9293cb8afad04ba2b31a2--