Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:107811 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 78288 invoked from network); 17 Nov 2019 21:35:17 -0000 Received: from unknown (HELO php-smtp3.php.net) (208.43.231.12) by pb1.pair.com with SMTP; 17 Nov 2019 21:35:17 -0000 Received: from php-smtp3.php.net (localhost [127.0.0.1]) by php-smtp3.php.net (Postfix) with ESMTP id 6463A2CE852 for ; Sun, 17 Nov 2019 11:27:45 -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,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE autolearn=no autolearn_force=no version=3.4.2 X-Spam-ASN: X-Spam-Virus: Error (Cannot connect to unix socket '/var/run/clamav/clamd.ctl': connect: Connection refused) Received: from mail-wr1-x432.google.com (mail-wr1-x432.google.com [IPv6:2a00:1450:4864:20::432]) (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 ; Sun, 17 Nov 2019 11:27:44 -0800 (PST) Received: by mail-wr1-x432.google.com with SMTP id w9so16976461wrr.0 for ; Sun, 17 Nov 2019 11:27:44 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=subject:to:references:from:message-id:date:user-agent:mime-version :in-reply-to:content-transfer-encoding:content-language; bh=tPp8eRnxrVPn96tSyTsDaFpCSEv4Ug89LJyZ9TMSwMk=; b=ijKw2UvUlQ+gWrLMEIM3fVq0UMVCkQXgzgvofB1wzANQhNye4UobRiYhqwYihI1/A5 Czv8EnVyXW1mxhnbKu3owKpAn9EhB+yfwnq7oHHl89N5J1omfnxeyojMHVSrRA06YS3D Z7m1tZMAJy6nA5eH9VQ0psWuJZjkejLXcsGA28jqaO3D9PH8ZywnjAUzmd8jZmAHogG/ I4NS6RkOzZg0R2762ZotVUT2Oqg0o/LuEsK+8GZHtLyv91IJiG+dikzKIbKRaH8/YXyx 76ELmxHmd2X2wHnkpG4HSg3+84fKlJJ5/SquZ3497udnVe5DTbnoKdCXc3/jtPO+aObD +9cg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-transfer-encoding :content-language; bh=tPp8eRnxrVPn96tSyTsDaFpCSEv4Ug89LJyZ9TMSwMk=; b=cv9NuQwpC9jQtN1eIeUVG17XK/uOdou+V0+jrlKTt6iSQC53WKrmJqXZ0aNJwyiBES mZqmGwxQua3k9YiHotHKmsL9p/Hr5MOzUx3e1fq4D05YJuFoB1Vnzq5VuJTIKYfNxhxG oF8cBat4zXmy9bXpMEQt6p+l5wccR75f7VXx/go6T6/XM+SGUoHmPFLVEP7Zk1iRqyek vOLQsRSTTbT/Vw7AYc9VTbca1+VP3t5ehnXvZv9B0+aN2d6e1SOqUK+MDEZ3TXJLpXaZ EezfdPS8ncQSZmmkFJFV2bVKNkYJlZ6xAlOfA3k2E97/GbGNosopE9JL+mPMnEXswplZ lZpA== X-Gm-Message-State: APjAAAVu8yP0isdEytI7EYei41NeC/Cu01SfisRVtZyoie2ftnYuhA2I Dd3PBl1aiJyy1lfFr+i99xEEglwn X-Google-Smtp-Source: APXvYqzUsSibnJaQQ9OT2OKvKMIIYvMIyj+TnGWKsL25/ytGgTsyEU2OxdfgERRKDewgLdzZVsIz7w== X-Received: by 2002:a5d:5404:: with SMTP id g4mr23259430wrv.359.1574018862818; Sun, 17 Nov 2019 11:27:42 -0800 (PST) Received: from [192.168.0.14] (cpc84253-brig22-2-0-cust114.3-3.cable.virginm.net. [81.108.141.115]) by smtp.googlemail.com with ESMTPSA id a17sm6681378wrs.33.2019.11.17.11.27.41 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sun, 17 Nov 2019 11:27:42 -0800 (PST) To: PHP Internals References: <461958fe-182e-59bf-9fdb-b110fd09e2b2@aimeos.com> <08de5448-3c5a-ad32-555e-b8f9579c1337@aimeos.com> Message-ID: <7b527bbb-47e3-9b24-8766-c15e6334c78a@gmail.com> Date: Sun, 17 Nov 2019 19:27:41 +0000 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:60.0) Gecko/20100101 Thunderbird/60.9.1 MIME-Version: 1.0 In-Reply-To: <08de5448-3c5a-ad32-555e-b8f9579c1337@aimeos.com> Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit Content-Language: en-GB X-Envelope-From: Subject: Re: [PHP-DEV] Concept: "arrayable" pseudo type and \Arrayable interface From: rowan.collins@gmail.com (Rowan Tommins) [Note: I've included the full text of the previous message as it wasn't sent to the list.] On 17/11/2019 18:35, Aimeos | Norbert Sendetzky wrote: >> It feels like there are two alternative suggestions here. Not only do >> they use the same keyword to mean different things, but "convertible to >> array", and "usable as array" seem like distinct concepts to me. > The name of the interface isn't perfect because it may be too close to > the type hint and therefore misleading. Suggestions for a better name > are always welcome. > > The concept contains three parts: > - arrayable type hint: Use objects and arrays alike > - \Arrayable interface: To enable implementing objects that are "arrayable" > - __toArray(): To convert objects to arrays Like I say, the name is part of it, but the purposes also overlap the way you describe them. It makes more sense to me to have two separate features for two separate use cases: - If I just want to know that the object I've received can be iterated, counted, and accessed using square brackets, then I need either a special type hint or a new interface, but not both. - On the other hand, if I just want to know I can call toArray or __toArray on an object, I don't care whether it implements Iterator, Countable, etc as well. >> For the "convertible to array" case, I think __toArray, or an interface >> specifying just that one method, would make more sense than combining it >> with the existing interfaces. I'm sceptical of that concept, though, >> because most objects could be converted to many different arrays in >> different circumstances, each of which should be given a different and >> descriptive name. > Can you give some examples for different conversions? Well, pretty much any method that returns array *could* be called "to array", but not many would be good candidates for such a generic name. You might return an array structure to be incorporated into a JSON response, or to be passed to a template renderer, or to map to database columns, etc. The only case I can think of where a generic "toArray" method would make sense is if you're creating a general-purpose "collection" or "list" object, in which case you're probably better off directly implementing methods like map and sort, rather than encouraging users to convert it back to a plain array. It doesn't seem like a common enough use case to need a new language feature, when it's simple enough to write "$foo->toArray()" without anything new. > The possibility to pass arrayable objects to the array_* methods should > be out of scope for the moment and is better discussed later to keep the > pieces small, I think. You are totally right, there may be some > unexpected behavior when doing that. The reason I mentioned it is that without it the new type hint or interface seems rather limited: I can't imagine many "array" type constraints being replaced with "arrayable" if you had to remember not to pass the variable to array_map, sort, etc. Regards, -- Rowan Tommins (né Collins) [IMSoP]