Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:123897 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 AB99F1A009C for ; Wed, 26 Jun 2024 21:24:03 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1719437121; bh=KuJHmZfHFw6W/JnuP9WTfywWpD0UahKRg9hyeUJFnjw=; h=In-Reply-To:References:Date:From:To:Subject:From; b=h+w4LXI4UtrsCTkXOBBW3/38VikcnxUfL1bOejeP0U5UOxjRkg6NXvPyS6wtns3da 98zl/imibnPkGDamwsvk3qcTdbwcVJpeXY//vcVoSCj8u70K6xzspnt/e9Q/1LfBZV 1GDXFSVVHkpqbsDywhcnrB7J0TUQyihU7P5KTnijpL6WoehoWdB7dbDDQYuvSrvMWt 9LnankQwuanlR1UCx0TAVPwxSERVMWuPioU2BHwAg4T3IYjFFlPRL6vdLPg+8nvL17 36aCywjUNyQQej4FmRsw41ROasE0fX3046MgB1YXwQpFxUvrlAz3VFkQYbS0XtCFC6 1GGjmtU+g1h3g== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 82B02180601 for ; Wed, 26 Jun 2024 21:25:20 +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.1 required=5.0 tests=BAYES_50,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,DMARC_MISSING,HTML_MESSAGE, RCVD_IN_DNSWL_LOW,SPF_HELO_PASS,SPF_PASS,T_KAM_HTML_FONT_INVALID, 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 fhigh2-smtp.messagingengine.com (fhigh2-smtp.messagingengine.com [103.168.172.153]) (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 ; Wed, 26 Jun 2024 21:25:19 +0000 (UTC) Received: from compute1.internal (compute1.nyi.internal [10.202.2.41]) by mailfhigh.nyi.internal (Postfix) with ESMTP id F40781140191 for ; Wed, 26 Jun 2024 17:24:01 -0400 (EDT) Received: from imap49 ([10.202.2.99]) by compute1.internal (MEProxy); Wed, 26 Jun 2024 17:24:01 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bottled.codes; h=cc:content-type:content-type:date:date:from:from:in-reply-to :in-reply-to:message-id:mime-version:references:reply-to:subject :subject:to:to; s=fm3; t=1719437041; x=1719523441; bh=H67oLX4iot i3+bMprMlbein6ahc3D88VK8vqlFJAANo=; b=st6hDgjq5XRscE/FTN+cJXZv3h Xki8lp95tQQchslUqKE/12qHvP4o35a5Wea6gt4FNrYz6LqLjQdczu1w2hkSkX2m f5RhVUtJGQ+Ylwcqmn40Wg6JVUqIbHnwpIm5mme0Kxfby3Esor9nbQWT4qzkDEEN GcsHQB49z+sBH0FQLnbC9fr5k3CUfE4jBbGfTdae7oL6ylgGRCRLlsGK9M8QNgJc 0Tclty1MrgswYK12r3FVFLjGVU3UE4R46HrWz8nTjPSjRUnBhI1AIql0qqvkUqXP mWROEkPhO0r83ZK+HjccCTLEVgTYI9KxQ2MzbYMC1CAFIrMqMVgcBiSoN93Q== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-type:content-type:date:date :feedback-id:feedback-id:from:from:in-reply-to:in-reply-to :message-id:mime-version:references:reply-to:subject:subject:to :to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s= fm2; t=1719437041; x=1719523441; bh=H67oLX4ioti3+bMprMlbein6ahc3 D88VK8vqlFJAANo=; b=flgsVgq1sHl8p7QV6cetj8mROeedOYr7w2vMIi/tlzwg DXg83AR/9GcnZAbWvgcNIAbRbPNqWsLNxG0ysUwLM/bZcgulyNaM43gipAfX7maL zRGMb45ss0ViWSgqNmVWmMrp4sYe/0hCEDySLeS49mwSUSurLbJtH8pZ7wSnD1b4 i+VT7F/QjRBwxs9EZnpsqnLcDpDaFZFiROQGER+MyrUjX8vTnh1FGlGpdLi1IuKe daS80z/5UQm0ukAarcY1Uf+f6OMJGOEPGLz65YyBGdlLeWpNo5zWaMJhsZzSIH/L mprLDFj2zfHEyt+TRxH8x87dkLOYl0G9z78FgBHp2g== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeeftddrtddvgdduiedvucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucenucfjughrpefofgggkfgjfhffhffvufgtsegrtd erreerreejnecuhfhrohhmpedftfhosgcunfgrnhguvghrshdfuceorhhosgessghothht lhgvugdrtghouggvsheqnecuggftrfgrthhtvghrnhepfeefudfhudduieekkedugffhud fgleejgfekgefhvdeikeelvddvjeehteegteegnecuvehluhhsthgvrhfuihiivgeptden ucfrrghrrghmpehmrghilhhfrhhomheprhhosgessghothhtlhgvugdrtghouggvsh X-ME-Proxy: Feedback-ID: ifab94697:Fastmail Received: by mailuser.nyi.internal (Postfix, from userid 501) id A077815A0092; Wed, 26 Jun 2024 17:24:01 -0400 (EDT) X-Mailer: MessagingEngine.com Webmail Interface User-Agent: Cyrus-JMAP/3.11.0-alpha0-538-g1508afaa2-fm-20240616.001-g1508afaa Precedence: bulk list-help: list-post: List-Id: internals.lists.php.net MIME-Version: 1.0 Message-ID: <3fb543ad-a497-43c4-952e-8842b4e11e27@app.fastmail.com> In-Reply-To: <71dc8137-ded0-41dc-bc9b-32dde12f9170@app.fastmail.com> References: <6D0733B2-A1D1-45EC-B93E-029B1616A22F@miles.systems> <71dc8137-ded0-41dc-bc9b-32dde12f9170@app.fastmail.com> Date: Wed, 26 Jun 2024 23:23:40 +0200 To: internals@lists.php.net Subject: Re: [PHP-DEV] [Initial Feedback] Typed Arrays Content-Type: multipart/alternative; boundary=bf8ab864c0d1441e95f02e409d29fccd From: rob@bottled.codes ("Rob Landers") --bf8ab864c0d1441e95f02e409d29fccd Content-Type: text/plain;charset=utf-8 Content-Transfer-Encoding: quoted-printable On Wed, Jun 26, 2024, at 22:56, Casper Langemeijer wrote: >=20 > On Wed, Jun 26, 2024, at 21:59, Richard Miles wrote: >> I think we should have typed arrays in PHP.=20 >=20 > Yes! I cannot stand sitting through conference talks on 'generics' tha= t only talk about 'collections'. This could be solved if we had typed ar= rays. If anything we would get better talks on Generics. :-) >=20 > Arrays of a type is one of the last cases where I need docblocks to te= ll my editor about the types. >=20 > In my opinion, even if we would have some implementation of generics, = having typed arrays with a simple syntax would be awesome. >=20 > A syntax suggestion: >=20 > $array =3D stdClass[]; > class A { > public stdClass[] $array; > } >=20 > Adding an invalid array member should throw TypeError. >=20 > I know there are way more edge-case situations to think of (for exampl= e: if class B extends A, $b is of type B[], but holds only A's, can $b b= e assigned as value of public A[] $a ?) >=20 >> Generics or bust. >=20 > I do not understand the reasoning behind that. Is it because we really= want generics, but when the 95% use-case is solved we fear that there w= ould not be enough momentum to get that? I'd love to have generics too, = but a very simple array syntax would in my opinion still add a lot of va= lue, even if we already had generics. >=20 You actually just gave me an evil idea on how to get generics in userlan= d=E2=80=A6 unfortunately it is bed time as I have an early train to catc= h in the morning. I will stew on it, but here is the gist: Create a class that generates a typed collection when accessed via array= , such that (new InternalTypedArray)['int'] produces a class that acts l= ike an array but only accepts ints. Using the composer file loading key, load a file that contains: define('TypedArray', new InternalTypedArray); You can now use $arr =3D TypedArray['MyType'] Could be interesting and I wouldn=E2=80=99t be surprised if it hasn=E2=80= =99t been done before.=20 =E2=80=94 Rob --bf8ab864c0d1441e95f02e409d29fccd Content-Type: text/html;charset=utf-8 Content-Transfer-Encoding: quoted-printable

=

On Wed, Jun 26, 2024, at 22:56, Casper Langemeijer w= rote:

<= /div>
On Wed, Jun 26,= 2024, at 21:59, Richard Miles wrote:
 I think we should have typed a= rrays in PHP. 

Yes= ! I cannot stand sitting through conference talks on 'generics' that onl= y talk about 'collections'. This could be solved if we had typed arrays.= If anything we would get better talks on Generics. :-)
Arrays of a type is one of the last cases where I need docb= locks to tell my editor about the types.

In= my opinion, even if we would have some implementation of generics, havi= ng typed arrays with a simple syntax would be awesome.
A syntax suggestion:

$array =3D= stdClass[];
class A {
  public stdClas= s[] $array;
}

Adding an inval= id array member should throw TypeError.

I k= now there are way more edge-case situations to think of (for example: if= class B extends A, $b is of type B[], but holds only A's, can $b be ass= igned as value of public A[] $a ?)

 Generics or bust.
I do not understand the reasoning behind that. Is it because= we really want generics, but when the 95% use-case is solved we fear th= at there would not be enough momentum to get that? I'd love to have gene= rics too, but a very simple array syntax would in my opinion still add a= lot of value, even if we already had generics.

=

You actually just gave me an evil idea = on how to get generics in userland=E2=80=A6 unfortunately it is bed time= as I have an early train to catch in the morning. I will stew on it, bu= t here is the gist:

Create a class that gen= erates a typed collection when accessed via array, such that (new Intern= alTypedArray)['int'] produces a class that acts like an array but only a= ccepts ints.

Using the composer file loadin= g key, load a file that contains:

define('T= ypedArray', new InternalTypedArray);

You ca= n now use $arr =3D TypedArray['MyType']

Cou= ld be interesting and I wouldn=E2=80=99t be surprised if it hasn=E2=80=99= t been done before. 

=E2= =80=94 Rob
--bf8ab864c0d1441e95f02e409d29fccd--