Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:107837 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 2726 invoked from network); 19 Nov 2019 21:07:41 -0000 Received: from unknown (HELO php-smtp3.php.net) (208.43.231.12) by pb1.pair.com with SMTP; 19 Nov 2019 21:07:41 -0000 Received: from php-smtp3.php.net (localhost [127.0.0.1]) by php-smtp3.php.net (Postfix) with ESMTP id B37FA2C2996 for ; Tue, 19 Nov 2019 11:00:39 -0800 (PST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on php-smtp3.php.net X-Spam-Level: X-Spam-Status: No, score=-2.0 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FROM,HTML_MESSAGE,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE autolearn=no autolearn_force=no version=3.4.2 X-Spam-ASN: AS3215 2.6.0.0/16 X-Spam-Virus: Error (Cannot connect to unix socket '/var/run/clamav/clamd.ctl': connect: Connection refused) Received: from mail-io1-xd2f.google.com (mail-io1-xd2f.google.com [IPv6:2607:f8b0:4864:20::d2f]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by php-smtp3.php.net (Postfix) with ESMTPS for ; Tue, 19 Nov 2019 11:00:39 -0800 (PST) Received: by mail-io1-xd2f.google.com with SMTP id k13so24462904ioa.9 for ; Tue, 19 Nov 2019 11:00:39 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to; bh=Njw8JbWZvsMf4riBOPonywsuau+hmDxlPVSAN/nVMuk=; b=QAiQfnm34WQJdSofwCdlfmqgDafbH7F6spoMjqJhjJ8oL1pVJj2xWSGQsNzCFZBanJ I4YZsZPG3FakbhImGPkPG4DCCgsls3PKSmXInm1JHt6BNUvryjplkklcVMsvwIrKQrx2 hfkGabnbWPL9GNzFSBCphQn2xKsReqA6nkkOTtW+erk3RAFqARkkVoNiZty3hFZk2rB/ HVMJEyVDlpvWeEIjBFtCmFmpD7TwR+apW1k9A+zHcetHcnVGQ/kfyLIyZRmJAirFQ28o AYzqLgxYZ5SXRpKnZJnGBg4XnTD9Gp0rZKa/uR/dmjuWl49c+DyW5/ZBQj2X5wHoE3RJ gdwQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to; bh=Njw8JbWZvsMf4riBOPonywsuau+hmDxlPVSAN/nVMuk=; b=byXGbNAoiS9zqEi7lfd7KJ19ztOBELgse62Z7Ar+WuB2bt8YIlC7Nu4cGIryBp7em5 K3s4rAaSbNejEPJcifaeezY6rZgfhW4b0DK2M6wsYQlMcHPoo2x8ILIUp4QHnVIOmYST 6hpP65VHaqNxeYlxfFrezCm/aHfkoA7zH0sJCFjIf2Kn1wJG4BnkrrhLw0IY1U374ljD ro22bv6ARQZwzNwVm0E10MFp2WffBNeT86L/u1+HL5RPrB82kEAgFn73HoeRiNpG1oFf rovBHVmB42nC/jvANnt7fUHSkyp7MboCnW/2Reov45m0YHek2QNeYYZyZM8ACsFPDuz7 2khQ== X-Gm-Message-State: APjAAAV6QNsSk1gPWGv5pBujGIsKTrLQQZNP2qbinc1QgBaQvqokdNR+ MzLNZD7pv2PduPCkK/DvC3mLChL+pISxDdatTeghh/rC X-Google-Smtp-Source: APXvYqzE9SHMt2jLwyfByMTYi/26wbEqr7Agdj7lPYebp39zWS+N+vVqA27cqM9VeD/7FxU5TiU1U8EGfg81uJq9Jss= X-Received: by 2002:a05:6638:626:: with SMTP id h6mr19581154jar.113.1574190038202; Tue, 19 Nov 2019 11:00:38 -0800 (PST) MIME-Version: 1.0 References: <461958fe-182e-59bf-9fdb-b110fd09e2b2@aimeos.com> <08de5448-3c5a-ad32-555e-b8f9579c1337@aimeos.com> <7b527bbb-47e3-9b24-8766-c15e6334c78a@gmail.com> <1BF6C8EC-6DF1-4481-94B2-3A6E7D1312BE@newclarity.net> <2814FAA1-3BEC-47CF-A95E-AE61A33B7308@newclarity.net> <83307615-4A87-4ACA-B450-3405DD9221D7@newclarity.net> In-Reply-To: <83307615-4A87-4ACA-B450-3405DD9221D7@newclarity.net> Date: Tue, 19 Nov 2019 19:00:26 +0000 Message-ID: To: PHP Internals Content-Type: multipart/alternative; boundary="000000000000093ff10597b7ae11" X-Envelope-From: Subject: Re: [PHP-DEV] Concept: "arrayable" pseudo type and \Arrayable interface From: rowan.collins@gmail.com (Rowan Tommins) --000000000000093ff10597b7ae11 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Tue, 19 Nov 2019 at 17:58, Mike Schinkel wrote: > But AFAIK no other "better" features were proposed. Do you have an > alternate to propose for __toArray()? I am all ears. > It's hard to know without understanding the use cases, which is why I got so hung up on your "consistency" argument - I wanted to understand what you were trying to achieve, and if this was the best way to achieve it. One thing you mentioned earlier was implicit interfaces, i.e. being able to match "any object that has a foo() method" rather than "any object that opted into the Fooable interface". You seemed to be saying that __toArray() would bring similar advantages, but maybe if we had implicit interfaces, those advantages would already be there? I may have completely misunderstood, but that's just an example of the wider thinking I'm talking about. > Said another way, if __toString() was not in PHP then no implicit casting > would be possible and thus nobody would do it. > Not quite. What I meant was that there are lots of places which implicitly cast *something* to string, and would still do so if we didn't have __toString(). For instance, it's common to use an integer in a double-quoted string or echo statement, which will implicitly cast it to string, so being able to use an object in the same place feels natural and useful. > The feels like "perfect being the enemy of the good" again. > ... > Also consider that implementation of _all_ casts would be a heavier lift > than implementing just one today, and then hopefully implementing others > later. > Possibly, but it's also about how we *prioritise* features. If the general plan is "eventually we hope to have overloads for all casts, but let's do them one at a time", we should have some reason why array comes next. My gut feel is that implicit casts to boolean (e.g. in if statements) are the next most common after strings, so if asked to prioritise, I would pick that over arrays. There's also a kind of "tipping point" where splitting up the feature no longer makes sense - it would be weird to have __toString, __toArray, and __toBool; then add __toInt; then wait a release to add __toFloat. So I guess my suggestion is that maybe we've already reached that tipping point, and rather than bikeshedding the order to add things in, we should go ahead and add them all. But maybe there is a reason that __toArray() is easier / less dangerous / more useful than the rest. > Seriously, when operators can be overloaded you can end up with code that > is almost impossible to decipher. But I do not think the same is true fo= r > casting as I tried to illustrate by example. > I guess this is where I'm less optimistic; I think overloaded casts could be just as unreadable as any other overloaded operator if abused - and just as powerful if used appropriately. I'd actually really love to be able to write $price =3D new Money('GBP', 10= 0, 0) + new Money('GBP', 50, 50); > I would like to err on the side of power combined with documentation and > education rather than prohibition. > Just to play devil's advocate, the same reasoning could be used to dismiss your dislike of full operator overloading. > You assume that my proposed __asArray() was not also implemented. > > However =E2=80=94 per the proposal =E2=80=94 when __asArray() returns tr= ue PHP would > treat the object as an array in any context where an array is expected bu= t > still allow methods to be called. > OK, I'd completely missed that proposal. It's an interesting idea, but it feels like there are some rough edges: why would you implement the function but return false, and how would that behave? And what if it returned true but there was no __toArray? Also, would the function actually receive the object, or the result of calling __toArray() on it? Would it be different in strict_types mode? In keeping with the idea of one feature at a time, it would probably make sense to work out the details of that mechanism for strings first, since we already have __toString(), then extend both parts to arrays at the same time. > > $allowed_html_builder->build()); > > But the primary use-case for __toArray() I have identified is to support > patterns that exist in other peoples code =E2=80=94 such as WordPress cor= e =E2=80=94 where > you cannot easily rearchitect the code to be what a developer thinks they > really need; you have to go with what is not what you want to be. > My example requires exactly the same interaction with the third-party code as your example - create a custom object, and call some methods on it. If the __toArray() call is completely automatic, then your example saves the 9 characters of "->build()", but it doesn't change the *architecture* of the solution. > > That doesn't mean the version you wrote is *wrong*, but should make us > > consider why one version deserves special treatment by the language and > the > > other one doesn't. > > What other version, and what special treatment is needed? > The version using "$foo->build()" rather than "(array)$foo", and the special treatment of "(array)$foo" actually calling "$foo->__toArray()". > I think I am sensing a pattern in your objections: For you, providing > benefits in one area is to be avoided unless all areas can receive simila= r > benefits? Almost. It's more that I'm against adding a large number of small features that each make one use case slightly easier, but don't generalise very well= . > ... even if the areas proposed to be improved represent the ~80 > percentile of use-cases? > Quite the opposite! I'm sceptical of this feature precisely because I think the use cases for it are rather narrow. Regards, --=20 Rowan Tommins [IMSoP] --000000000000093ff10597b7ae11--