pon., 1 lip 2024 o 03:01 Larry Garfield <la...@garfieldtech.com> napisał(a):

> 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
>

Considering other languages it is worth mentioning that Java and C# have
generics AND typed arrays that act as a typed list/seq so sure it doesn't
solve all problems otherwise these languages wouldn't have both of them.

>From my personal experience, a typed list/seq solves most cases where I
need the elements of an array to be strictly instances of a specific type.
What I see typed list/seq like `string[]` has more ergonomics, it can be
used to interact between libraries and application code without creating
intermediate objects, while collection<Dict> proposal always requires
declaring type - this simply multiplies the number of declared types and
create more coupling where it is not needed! Also, most cases I work with
are just fine with using just foreach or array_ family functions.

I believe typed list/seq could also be used to implement custom collection
like:

class Article {
    function __construct(public string $title) {}
}

// collection(Seq) Articles<Article> {
// }

final class Articles {
    public function __construct(protected Article[] $articles = []) {}
    public function getIterator(): Traversable {
        return new ArrayIterator($this->articles);
    }
} // or extend any other collection class with just typed list/seq property

This doesn't have to collide with generics in any way, as mentioned before
in the thread these features may exist simultaneously, typed list/seq can
translate in the future to a generic type.
I believe it'd make people's lives easier if we could have this in place
soon.
Don't you agree?

Cheers,
Michał Marcin Brzuchalski

Reply via email to