Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:108398 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 89207 invoked from network); 4 Feb 2020 19:53:43 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 4 Feb 2020 19:53:43 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 160EC18059D for ; Tue, 4 Feb 2020 10:05:56 -0800 (PST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,SPF_HELO_NONE, SPF_NONE autolearn=no autolearn_force=no version=3.4.2 X-Spam-ASN: AS34788 85.13.163.0/24 X-Spam-Virus: No X-Envelope-From: Received: from dd46610.kasserver.com (dd46610.kasserver.com [85.13.163.220]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Tue, 4 Feb 2020 10:05:55 -0800 (PST) Received: from [192.168.178.23] (x4d01e038.dyn.telefonica.de [77.1.224.56]) by dd46610.kasserver.com (Postfix) with ESMTPSA id 703514340043; Tue, 4 Feb 2020 19:05:53 +0100 (CET) To: Larry Garfield , php internals References: <861afae0-4568-745f-6615-a252067cc506@aimeos.com> <5f5a4ab3-e609-4324-b056-53f782f1a63b@www.fastmail.com> Organization: Aimeos GmbH Message-ID: Date: Tue, 4 Feb 2020 19:05:52 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.4.1 MIME-Version: 1.0 In-Reply-To: <5f5a4ab3-e609-4324-b056-53f782f1a63b@www.fastmail.com> Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 7bit Subject: Re: [PHP-DEV] Re: [RFC] "arrayable" pseudo type hint From: norbert@aimeos.com (Aimeos | Norbert Sendetzky) Am 04.02.20 um 18:18 schrieb Larry Garfield: >> interface Arrayable extends ArrayAccess, Countable, Traversable >> { >> public function toArray() : array; >> } >> >> Then, methods signatures can support array and Array-like objects: >> >> function useArrayable( array|Arrayable $arg ) : array|Arrayable { >> $cnt = count( $arg ); >> $value = $arg['key']; >> foreach( $arg as $key => $entry ); >> is_object( $arg ) { $nativeArray = $arg->toArray(); } >> return $arg; >> } >> >> If union data types are available and "iterable" is implemented as >> alias, an alias "arrayable" for "array|Arrayable" may be added as >> well. > > The more I think on it, the less I like `arrayable`. PHP arrays are > a terrible data structure from a type system point of view, with too > much functionality crammed into one variable type. The array internals are outside the scope of this proposal and are not affected in any way by this proposal. > If you want an object that shoves all the various bits of arrayness > into one object, there's already ArrayObject, which you can extend if > desired. Tried that already, but other core developers made very clear that ArrayObject is one of the worst implementations in PHP and that it's not going to be improved or touched in any way and using it is highly discouraged. > Especially with union types, amalgam built in types like this are > even less needed. If an "arrayable" pseudo type should be implemented is subject to discussion. It would be a short form for "array|Arrayable" (native type or interface for array like objects) only offering a syntactical sugar for developers. > Aside from iterable, the only other built-in alias I would see is one > for array|ArrayAccess. But... it's now possible to do exactly like > that, so I don't know how useful it would be. "array|ArrayAccess" is not sufficient because arrays are also countable and traversable and this isn't possible with union data types: interface Arrayable extends ArrayAccess, Countable, Traversable function useArrayable( array|Arrayable $arg ) { $cnt = count( $arg ); $value = $arg['key']; foreach( $arg as $key => $entry ); }