[go: nahoru, domu]

Re: [Initial Feedback] Typed Arrays

From: Date: Mon, 01 Jul 2024 00:59:53 +0000
Subject: Re: [Initial Feedback] Typed Arrays
References: 1 2 3 4 5 6  Groups: php.internals 
On Sun, Jun 30, 2024, at 11:13 AM, Michał Marcin Brzuchalski wrote:
> Hi Richard,
>
> czw., 27 cze 2024, 22:33 użytkownik Richard Miles 
> <richard@miles.systems> napisał:
>> 
>> > I worked with Joe Watkins to do a proof-of-concept for generic traits.
>> > It's a bit old since it's from 2017, but could be a useful starting
>> > point if you are serious about pursuing this idea:
>> > 
>> > https://github.com/php/php-src/compare/master...morrisonlevi:php-src:parameterized_traits
>> 
>> 
>> I’m also interested in this; it will help see branches like these.
>> Did you ever get the POC working? What did you feel like was the biggest hurdle?
>
> There even was an RFC in voting which Joe implemented and it addresses 
> nearly what is discussed it this thread https://wiki.php.net/rfc/arrayof
>
> I must admit that the collection<Dict> proposal is bit too complex to 
> address such tiny but powerfully in my opinion functionality which is 
> typed array. What Derick showed looks more like generic collection and 
> such require declaring it's type. In my opinion this is way too mush 
> hassle to declare as many collection types as many usages in 
> application. Also two collection types with the same collection item 
> type will not be compatible from type perspective. While typed array 
> seems much more clear and compatible in all places where typed array is 
> needed without declaring separate type for each usage.
>
> If I were to choose between typed-array and collection like Derick 
> showed a little bit I'd choose typed arrays definitely as a first 
> feature to be merged into PHP.
>
> Cheers,
> Michał Marcin Brzuchalski

Contextual point: Nearly every other major language at this point that has a meaningful standard library has gone with a three-separate-object approach (Seq, Set, Dict, called various things).  At least Python, Javascript, Rust, Kotlin, and Swift, in my research.  (Go doesn't, but Go avoids having features by design.)  And AFAIK *every* language except PHP and Lua separates sequences from dictionaries.  Typed arrays will not full resolve the seq vs dict problem, which is arguably PHP's original sin.  And there's a lot of weird issues to resolve around what the syntax could even be, since PHP has untyped variables.

The custom collection syntax Derick has been working on is a second-best alternative to generics, essentially.  It is less ergonomic, no question, but can also be implemented without generics, and so is about 5x easier to do.  If we could get native generics, then I think everyone involved agrees building collections off of that -- in essentially the same way as every language I mentioned above --- would be preferable to a custom one-off syntax.

--Larry Garfield

« previous php.internals (#124120) next »