Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:86202 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 94782 invoked from network); 13 May 2015 23:03:06 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 13 May 2015 23:03:06 -0000 Authentication-Results: pb1.pair.com smtp.mail=guiwoda@gmail.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=guiwoda@gmail.com; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.213.170 as permitted sender) X-PHP-List-Original-Sender: guiwoda@gmail.com X-Host-Fingerprint: 209.85.213.170 mail-ig0-f170.google.com Received: from [209.85.213.170] ([209.85.213.170:36101] helo=mail-ig0-f170.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 35/80-24392-928D3555 for ; Wed, 13 May 2015 19:03:06 -0400 Received: by igbpi8 with SMTP id pi8so152827097igb.1 for ; Wed, 13 May 2015 16:03:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :content-type; bh=TTTy82Kk65Rgra8IOHXZPEPHqRQKEvhrCVm4Dtniaws=; b=Z/fhTHxp9lXgpdQfQxZfrekeSd9i1kN0i6zD5rRerr98wTGQGR3pImAkEPC+wP/r08 w66+M0/n2Hm8M2hoxMzsWB1hB0iRpytXyoUun+OuG3z3F78iKpx9gK6MyClC8tq1drEV 0ecyp8shnS+lNMFLCPLqtJRY028+L0tcIHhzAcLjOfcYKcxPpKW7dlC2TrtaRSWVPYr3 vHOl7M1CabY8KRY5aEIEWn5NY1Ftha0bejuSeP/cjSjw/nxceCUa4ZWHA2ug3PtnJVhq GVugaJrbrvH1/D80R4riCKtqdENs52G7/GK0nExcR4nvPTVly+xTk8I+X8kYinUbKrLK GroA== X-Received: by 10.107.36.147 with SMTP id k141mr1605417iok.52.1431558182589; Wed, 13 May 2015 16:03:02 -0700 (PDT) MIME-Version: 1.0 References: <023f01d08dc4$fce2e8e0$f6a8baa0$@php.net> In-Reply-To: <023f01d08dc4$fce2e8e0$f6a8baa0$@php.net> Date: Wed, 13 May 2015 23:03:02 +0000 Message-ID: To: francois@php.net, internals@lists.php.net Content-Type: multipart/alternative; boundary=001a1141bbeef2dd2c0515fe9c2d Subject: Re: [PHP-DEV] Proposal: interfaces for object to scalar type casting From: guiwoda@gmail.com (Guido Contreras Woda) --001a1141bbeef2dd2c0515fe9c2d Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable @francois I understand your point, but I have a different view of the userland situation. Let me give some examples: In the array case, Iterator / Traversable doesn't solve casting, it solves iteration. Given a Traversable object from a third party library, if a method from another (or the same) third party library requires an array as an argument, the developer would have to iterate the object and build an array herself in a Mediator or Adapter class. If she could cast it, then all she would need to do is orchestrate the messaging, without fiddling in the middle. As for the other cases, there are plenty of examples that could prove that a userland object may want to have a different representation as a string than as other scalar types. Money is the first that comes to mind: $money =3D new Money(12.5, Money::DOLLARS); var_dump((string) $money); // string(5) "$12.5" var_dump((float) $money); // float(12.5) While there is information loss in the conversion, it is just a userland example that could well benefit from different representations in the various scalar types. For example, if an interest were to be calculated from a Money object, the currency wouldn't matter, and the interest calculator may as well expect just a float as argument. Also, there's a huge cognitive gap. For example, if a developer wants an object to be evaluated as a boolean, she would have to force an empty string as the __toString() method return. That by itself does not represent her intention. Without proper documentation, it's just a matter of time for another developer to find this behavior and remove it or replace it with something more representative of a string representation of the object. Cheers! El mi=C3=A9., 13 de may. de 2015 a la(s) 6:37 p. m., Fran=C3=A7ois Laupretr= e < francois@php.net> escribi=C3=B3: > Hi, > > > De : Guido Contreras Woda [mailto:guiwoda@gmail.com] > > > > Multiple frameworks and libraries define interfaces or objects that wra= p > > around some scalar value and add behavior, the most common case being > > arrays and Collection / Hash[Set|List] objects. Although this objects > > benefit from the possibility of having behavior themselves, sometimes t= he > > implementation is outside our project scope (an external dependency) an= d > > interaction between this object and other blocks of code that expect > scalar > > types usually needs to be controlled by the user, which adds the > necessity > > for adapters or some sort of mediator. > > > > Now that we have the ability to hint return types, a common interface > could > > be built that, given an object that implements it, lets userland code u= se > > this object as if it were the scalar type it is wrapping. Unboxing the > > object is pretty easy, and I wouldn't go as far as proposing interfaces > for > > rebuilding the object itself (not now, at least). > > > > So, what do you think of a set of interfaces that allow userland object= s > to > > be used as scalar types? > > First, we probably don't need anything for arrays. The Iterator interface > family already provides everything we need to consider an object as an > array. > > For other scalar types, while I agree casting should be supported, I don'= t > like the solution of creating new methods. I would prefer always calling > __toString() and then, convert the string to the desired type. So, > (int)$object would just be a shortcut for (int)(string)$object. Quite eas= y > to implement, no new method, just need to enrich the convert_to_xxx() > functions to go through convert_to_string first. I already had this idea > for 7.0 but missed time. Maybe for 7.1. > > I also think it's more consistent as an object could not be transformed t= o > different unrelated values when cast to different scalar types. If $objec= t, > when cast to int, becomes 10 and, when cast to string, becomes 'foo', is = it > consistent in such a loose typing system ? So, I prefer that, by > construction, (int)$obj always gives the same value as (int)(string)$obj. > > Regards > > Fran=C3=A7ois > > --001a1141bbeef2dd2c0515fe9c2d--