Re: Re: [RFC] "arrayable" pseudo type hint

From: Date: Tue, 04 Feb 2020 17:18:22 +0000
Subject: Re: Re: [RFC] "arrayable" pseudo type hint
References: 1 2  Groups: php.internals 
Request: Send a blank email to internals+get-108397@lists.php.net to get a copy of this message
On Tue, Feb 4, 2020, at 6:48 AM, Aimeos | Norbert Sendetzky wrote: > I would like to modify my initial concept of an "arrayable" type because > PHP core developers seems to be in favor of the upcoming union data > types instead of adding a new "arrayable" pseudo type similar to > "iterable". > > So, I would like to propose an "Arrayable" interface that combines > ArrayAccess, Countable and Traversable interfaces and adds a toArray() > method: > > 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; > } > > Adding a toArray() method also avoids controversy around using a magic > __toArray() method, even if this would be easier for developers in this > case. > > 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. 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. Especially with union types, amalgam built in types like this are even less needed. 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. --Larry Garfield

« previous php.internals (#108397) next »