Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:107854 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 63862 invoked from network); 24 Nov 2019 18:00:57 -0000 Received: from unknown (HELO php-smtp3.php.net) (208.43.231.12) by pb1.pair.com with SMTP; 24 Nov 2019 18:00:57 -0000 Received: from php-smtp3.php.net (localhost [127.0.0.1]) by php-smtp3.php.net (Postfix) with ESMTP id 24DA02C0465 for ; Sun, 24 Nov 2019 07:55:05 -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=-1.9 required=5.0 tests=BAYES_00,FROM_EXCESS_BASE64, RCVD_IN_DNSWL_NONE,SPF_HELO_PASS,UNPARSEABLE_RELAY autolearn=no autolearn_force=no version=3.4.2 X-Spam-ASN: AS16509 54.207.0.0/17 X-Spam-Virus: Error (Cannot connect to unix socket '/var/run/clamav/clamd.ctl': connect: Connection refused) Received: from smtpbgbr2.qq.com (smtpbgbr2.qq.com [54.207.22.56]) (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, 24 Nov 2019 07:55:03 -0800 (PST) X-QQ-mid:Yeas17t1574610889t726t21512 Received: from 14D73359B2E14ADDB4DDC3857AE32898 (me@jhdxr.com [220.255.44.152]) X-QQ-SSF:00000000000000F0F61000000000000 To: "'Aimeos | Norbert Sendetzky'" , References: <861afae0-4568-745f-6615-a252067cc506@aimeos.com> In-Reply-To: <861afae0-4568-745f-6615-a252067cc506@aimeos.com> Date: Sun, 24 Nov 2019 23:54:48 +0800 Message-ID: <003801d5a2df$801404e0$803c0ea0$@jhdxr.com> MIME-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Mailer: Microsoft Outlook 16.0 Thread-Index: AQInmQZs0nwdPtQQ0zSpqH5mdWxpG6b2h2RQ Content-Language: zh-cn X-QQ-SENDSIZE: 520 Feedback-ID: Yeas:jhdxr.com:qybgforeign:qybgforeign7 X-QQ-Bgrelay: 1 X-Envelope-From: Subject: RE: [PHP-DEV] [RFC] "arrayable" pseudo type hint From: me@jhdxr.com (=?utf-8?b?Q0hVIFpoYW93ZWk=?=) > This proposal is not obsolete by the implementation of union types = (i.e. > array|Traversable) because union data types are types/interfaces > combined by OR. "arrayable" is a combination of OR and AND: >=20 > array|ArrayAccess&Countable&Traversable True. But it's also easy to implement in userland. Just create a new = interface that extends these three interfaces, then union types can = work. ```php interface Arrayable extends ArrayAccess, Countable, Traversable {} function array_x (array|Arrayable $arr) {} ``` This is not something that userland cannot implement or very difficult = to implement. IMO it will make php more complex if we include this in = the core. Actually what you suggested has been totally covered by the = future scopes of the union types RFC. If you really want to explore this = idea, I think they are worth more attention, instead of this specific = case.=20 Regards, CHU Zhaowei > -----Original Message----- > From: Aimeos | Norbert Sendetzky > Sent: Wednesday, November 20, 2019 6:21 AM > To: internals@lists.php.net > Subject: [PHP-DEV] [RFC] "arrayable" pseudo type hint >=20 > Since PHP 7.1, there's the "iterable" pseudo type hint that matches = "array" or > "Traversable". >=20 > PHP frameworks would profit from support of an "arrayable" pseudo type = hint > that matches "array" and all objects that can be used like arrays. >=20 > For that, "arrayable" objects have to implement these interfaces: > - ArrayAccess > - Countable > - Traversable (i.e. either Iterator or IteratorAggregate) >=20 >=20 > Implementing "arrayable" pseudo type, we could pass arrays or all = objects that > can be used like arrays to methods and do: >=20 > function useArrayable( arrayable $arg ) : arrayable { > $cnt =3D count( $arg ); > $value =3D $arg['key']; > foreach( $arg as $key =3D> $entry ); > return $arg; > } >=20 >=20 > Best use cases are: >=20 > - Laravel Collection > = (https://github.com/laravel/framework/blob/6.x/src/Illuminate/Support/Col= lect > ion.php) >=20 > - Aimeos Map (https://github.com/aimeos/map) >=20 >=20 > No new interface is proposed because we can check if objects = implement: >=20 > ArrayAccess && Countable && Traversable >=20 >=20 > Because "array" !=3D=3D "arrayable", "arrayable objects will not be = accepted by > array_* functions and all functions that only use "array" as type hint = for > parameters and return types. >=20 >=20 > This proposal is not obsolete by the implementation of union types = (i.e. > array|Traversable) because union data types are types/interfaces > combined by OR. "arrayable" is a combination of OR and AND: >=20 > array|ArrayAccess&Countable&Traversable >=20 >=20 > Also, "arrayable" won't supersede "iterable" because >=20 > - "iterable" matches arrays, iterators and generators >=20 > - "arrayable" matches arrays and objects that can be used like arrays >=20 >=20 > "Can be used like arrays" doesn't include support for empty() because = it works > for empty arrays but not for "arrayable" objects without elements. > If possible and requested, this may be addressed by another RFC. >=20 >=20 > As Larry Garfield suggested, this pre-RFC proposes concentrates on the > "arrayable" pseudo type only. >=20 > It does NOT discusses that: >=20 > - arrayable objects can be converted to arrays (Steven Wade works on = an RFC > for a __toArray() magic function) >=20 > - ArrayObject or any other arrayable object makes the array_* = functions less > relevant in the future >=20 > - arrayable objects can be passed to array_* functions in the future >=20 > -- > PHP Internals - PHP Runtime Development Mailing List To unsubscribe, = visit: > http://www.php.net/unsub.php >=20