Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:124127 X-Original-To: internals@lists.php.net Delivered-To: internals@lists.php.net Received: from php-smtp4.php.net (php-smtp4.php.net [45.112.84.5]) by qa.php.net (Postfix) with ESMTPS id 649E41A009C for ; Mon, 1 Jul 2024 04:48:29 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1719809389; bh=BhaYFeQfBJzF8eLU6zAgNW+JAyWVu+Y49cVXPQ0Dd4c=; h=References:In-Reply-To:From:Date:Subject:To:Cc:From; b=UwhHE5GYKYEmd/ZdESdv+zTAskZlI+9g0iQ+O6rAV4EoCSCO75GXkQiTzXs7pBYcO U/rF2awRGcsBucPaq+FX3RP/o8lEA/DZJuRMmmH5+1cuXDNBH9L3aLY+WZaapdUmek I8905EFmVKjZunsA98UQd/P/R4KicQqPC9sqfMT5Ymqo0a6Jw2QNVA9xZQYnPx+WNq PKZWrIwlv3YfBmj2lpk0LWTQtX8pgXhqjYySHkL0WsTzSeTTP7GwCB9EowLvNXqmM0 R9JDE9e8n+6o5B8EZGbPcrlQBiLrsmDk9MagsOomCNZfDaosqrhCxnCJlfF+kO1GdO ONmlsfXf05EPQ== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id A869D18003F for ; Mon, 1 Jul 2024 04:49:48 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 4.0.0 (2022-12-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=0.6 required=5.0 tests=BAYES_50,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,DMARC_PASS,FREEMAIL_FROM, HTML_MESSAGE,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE, SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=4.0.0 X-Spam-Virus: Error (Cannot connect to unix socket '/var/run/clamav/clamd.ctl': connect: Connection refused) X-Envelope-From: Received: from mail-yw1-f176.google.com (mail-yw1-f176.google.com [209.85.128.176]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Mon, 1 Jul 2024 04:49:48 +0000 (UTC) Received: by mail-yw1-f176.google.com with SMTP id 00721157ae682-64e81cd12cdso2607147b3.0 for ; Sun, 30 Jun 2024 21:48:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1719809307; x=1720414107; darn=lists.php.net; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=NrTXbsO9+hIiiYnfxH+NW8GPoOsKz8Cgv4wpKhA7pYY=; b=OJd8CyUqk/6IsCairU24SMOUIso4E8dG1jpmK/G63aIajAR9Mjtphc/rt4KGQxZUFE RDnBp3CX5tMFnNmIl4P8RH8//9ek8uG07ZOrLKVIvwWFTClSWInlHDESrgM2PGweSafF WjIhB1TtZW3uwSp+hB3Uv+caap/lKvw8z/OnHyJV3t1M0U+i0FcpHAOMgmRtEYpGMpVR z1ji6tVB3SXYrPrVX+aE02K+vN76x2vdo8nQZtUGnl1jv0Y6R/uox24D0GXAv0YRh6gq 67ms8sk7MiZoEynLquW9f1Okac8t2kSBjqXH0tuegaO//XxkvkEnBmjdVrFO4gGPHUUe ZClQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1719809307; x=1720414107; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=NrTXbsO9+hIiiYnfxH+NW8GPoOsKz8Cgv4wpKhA7pYY=; b=FZPdEE7tHzfIMwZTZDzGU67K7opVCsE//JAvAbPuCv7/G04bJEDaXgvTR1RP7TpbPV G3FCeV3ta0t+OCFsAn9VSjoy6fdeY+cy0Fy73wQo0sGKhHOSali3Y4h539HSyDTmsHX6 Husu4HhpcYzBZA1fFIx8TQYnlhtFuoENauQyrdHJ5GkuaXOmJtbxk05GuRaK0BK6T6gN i99NvitTTdswOVXE/R1Le1O5aENfd6g2p9CDyP7K59sUiT/LgCA506hvEB6IL3Nqx+pS kner5uypR0wi4rjLqRQOZr+fbCuK4TfG2bv3vQJyRhIkknajeldbs4Bq6SiBx/hhni6C IKyQ== X-Gm-Message-State: AOJu0Yx6Te5ZU6P7SDw7c8OyLzm0acIioQFhYGuCQ6N64qP3JXZ4VTNz y3GI5M8i+jv7oe+eDTF2D2IPAko9Jkkp0HETcMzcLtSFi+mYaK4yaUdZ5vTnuC9mKJ8Qulq/TKh +2E6yrj/897e3c6bGTT69o0twwEQ= X-Google-Smtp-Source: AGHT+IGYKo5gPlYXzhkh2YhuQsFH0eMdVyaRGz1lYugy7CGZkMv1TS+lexBIu74srYfZVS52NOJDDDM+I40mIJvBL6E= X-Received: by 2002:a81:a748:0:b0:627:e3ba:2ad7 with SMTP id 00721157ae682-64c6cabcb26mr25099647b3.9.1719809306987; Sun, 30 Jun 2024 21:48:26 -0700 (PDT) Precedence: bulk list-help: list-post: List-Id: internals.lists.php.net MIME-Version: 1.0 References: <4ECBB519-B129-426F-93AE-33738A1ABA06@miles.systems> <4996ae09-e5eb-4a8b-90e2-7461805c4cb7@app.fastmail.com> In-Reply-To: <4996ae09-e5eb-4a8b-90e2-7461805c4cb7@app.fastmail.com> Date: Mon, 1 Jul 2024 06:48:15 +0200 Message-ID: Subject: Re: [PHP-DEV] [Initial Feedback] Typed Arrays To: Larry Garfield Cc: php internals Content-Type: multipart/alternative; boundary="000000000000d3188d061c284ee6" From: michal.brzuchalski@gmail.com (=?UTF-8?Q?Micha=C5=82_Marcin_Brzuchalski?=) --000000000000d3188d061c284ee6 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable pon., 1 lip 2024 o 03:01 Larry Garfield napisa=C5= =82(a): > On Sun, Jun 30, 2024, at 11:13 AM, Micha=C5=82 Marcin Brzuchalski wrote: > > Hi Richard, > > > > czw., 27 cze 2024, 22:33 u=C5=BCytkownik Richard Miles > > napisa=C5=82: > >> > >> > I worked with Joe Watkins to do a proof-of-concept for generic trait= s. > >> > 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:para= meterized_traits > >> > >> > >> I=E2=80=99m also interested in this; it will help see branches like th= ese. > >> 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/arrayo= f > > > > I must admit that the collection 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=C5=82 Marcin Brzuchalski > > Contextual point: Nearly every other major language at this point that ha= s > 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 ful= l > resolve the seq vs dict problem, which is arguably PHP's original sin. A= nd > there's a lot of weird issues to resolve around what the syntax could eve= n > 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 t= o > do. If we could get native generics, then I think everyone involved agre= es > 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 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
{ // } final class Articles { public function __construct(protected Article[] $articles =3D []) {} 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=C5=82 Marcin Brzuchalski --000000000000d3188d061c284ee6 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable


=
pon., 1 lip 2024 o 03:01=C2=A0Larry G= arfield <larry@garfieldtech.co= m> napisa=C5=82(a):
On Sun, Jun 30, 2024, at 11:13 AM, Micha=C5=82 Marcin Brzuchalsk= i wrote:
> Hi Richard,
>
> czw., 27 cze 2024, 22:33 u=C5=BCytkownik Richard Miles
> <richard@miles.systems> napisa=C5=82:
>>
>> > I worked with Joe Watkins to do a proof-of-concept for generi= c traits.
>> > It's a bit old since it's from 2017, but could be a u= seful starting
>> > point if you are serious about pursuing this idea:
>> >
>> > https://github.com/php/php-src/compare/master...morrisonlevi:php-src:par= ameterized_traits
>>
>>
>> I=E2=80=99m also interested in this; it will help see branches lik= e these.
>> Did you ever get the POC working? What did you feel like was the b= iggest 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/rf= c/arrayof
>
> I must admit that the collection<Dict> proposal is bit too compl= ex 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 mu= sh
> hassle to declare as many collection types as many usages in
> application. Also two collection types with the same collection item <= br> > type will not be compatible from type perspective. While typed array <= br> > seems much more clear and compatible in all places where typed array i= s
> 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=C5=82 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 approac= h (Seq, Set, Dict, called various things).=C2=A0 At least Python, Javascrip= t, Rust, Kotlin, and Swift, in my research.=C2=A0 (Go doesn't, but Go a= voids having features by design.)=C2=A0 And AFAIK *every* language except P= HP and Lua separates sequences from dictionaries.=C2=A0 Typed arrays will n= ot full resolve the seq vs dict problem, which is arguably PHP's origin= al sin.=C2=A0 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 al= ternative to generics, essentially.=C2=A0 It is less ergonomic, no question= , but can also be implemented without generics, and so is about 5x easier t= o do.=C2=A0 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-of= f syntax.

--Larry Garfield

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

From my personal experience, a typed list/seq solves most cases=C2=A0= where I need the elements of an array to be strictly instances of a specifi= c type.
What I see typed list/seq like `string[]` has more ergono= mics, it can be used to interact between libraries and application code wit= hout creating intermediate objects, while collection<Dict> proposal a= lways requires declaring type - this simply multiplies the number of declar= ed 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 function= s.

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

class Article {<= br>=C2=A0 =C2=A0 function __construct(public string $title) {}
}

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

final class Articles {
=C2=A0 =C2=A0 public function __con= struct(protected Article[] $articles =3D []) {}
=C2=A0 =C2=A0 pub= lic function getIterator(): Traversable {
=C2=A0 =C2=A0 =C2=A0 =C2=A0 = return new ArrayIterator($this->articles);
=C2=A0 =C2=A0 }
} // o= r extend any other collection class with just typed list/seq property
=

This doesn't have to collide with generics in any w= ay, as mentioned before in the thread these features may exist simultaneous= ly, 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=C5=82 Marcin Brzuchalski
--000000000000d3188d061c284ee6--