Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:123903 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 567CF1ADDA5 for ; Wed, 26 Jun 2024 23:09:25 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1719443442; bh=DwIWFHaFHM3Zs8GGwAirE1Q8sxnaISpn6sA1O1DjhHQ=; h=References:In-Reply-To:From:Date:Subject:To:Cc:From; b=frS/zvL+oKhQYIMhQVSI41nX5GJQAQ5inWhRln/h0BF9Bp4A8Wh76KltcCgmWnhCp sMQImcxjRCCiSFN+vjZRUS/A2FpAEV8Fi+S7gVEz7Bf8vg9fXeBoM3LAeEMdWtiaIU vAcmbtBD11JmK19QH4ieEc2Bduhj2p/MdQ9C2ISwrQquLlPlU+ekqOJGw1asUEdRqM z3S4NeIRAFg0iN03vuSJNmVipb5bdREQvb2Dad5ZL9b67CgW3cINd9bWhXQObaL9Or eV1e4dqnC7TnrHIf3uE8QpzBsSY2wBtdbQqZlPqvshzz/Idgx66zpZLhdlKAHE5zmT +hnDhKLtpzewA== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 0B207180555 for ; Wed, 26 Jun 2024 23:10:41 +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,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-io1-f49.google.com (mail-io1-f49.google.com [209.85.166.49]) (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 23:10:39 +0000 (UTC) Received: by mail-io1-f49.google.com with SMTP id ca18e2360f4ac-7eb01106015so297088339f.1 for ; Wed, 26 Jun 2024 16:09:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=datadoghq.com; s=google; t=1719443361; x=1720048161; darn=lists.php.net; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=/q/eeDIIrg2kgWourgKwD1G/EzS2k4ibJ2OuahgNExs=; b=NAHBYV98DEPq6qYD+c5PRLqVHKynyj10oSSXSQXO+bfdffkFSev7waQ+rymdS70p2Y 4a9FaPe1iNtQUWdO667ZSLb68y6Nlvk0OMXt4s4DAQyARzTYDzqQYlDCRoFdKgLLDciQ FYdbVOef/dJozch3IuvNUhpFK5HVD0ToQ5LEo= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1719443361; x=1720048161; h=content-transfer-encoding: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=/q/eeDIIrg2kgWourgKwD1G/EzS2k4ibJ2OuahgNExs=; b=XyUlbfugkmhATZYkGSwbS2uCgSyVA6jyNMeYEDLCpvol8QrqOEj9TZN7ghGxc4Fl3z m+Kp8bqNRoFDiaIFNDJitpzXWc74DLbumcbt+MvSXZ0hl7FGM+eaTODzWtwx0vgg1FT7 +8+JP2PZGRueK015s/VN75OnUsX2+/2MGPUJGABq2j9MjcvDQRs1dd32nnxNtrKrygnV cLPhjHWpTACs2BPGoIZjT4Rx8by5wLJYWKPMK1m5DH9ekm1jE9BudJVRtpjyzPNf2ff0 Sz0Dm9tbrqeHQ+TRHWG8iNWd/ZPDuilCUX0EoF69n21I1/cTw2d2Tb3+LVPgwoIeteQA pwUg== X-Gm-Message-State: AOJu0YyrLY9EpLnK5+mMKkXQEawsoiYq96b5SOWvBWpg+j2cEumzJbYi /vxvfl7eHM7St0dGJGOv66/EGxNxSvT8D/6QMNWfcz5x5QZq0t0+7H+wdr9P1C4Z/mAaqZfSp8+ P2SKg5DUSfoIT5VuffOGAXPgOUSpRdnw7uRhqU09I+CdpnkEt X-Google-Smtp-Source: AGHT+IGcVZAJrvZcXo5ASJdwd1Uqip4AJeCkgE339Uo/Yh+XHoH+cKbTNgm1pVR28vW9J4G05lI/vekCWaHNYlP08xI= X-Received: by 2002:a05:6602:341e:b0:7eb:8730:a304 with SMTP id ca18e2360f4ac-7f3a7532c36mr1342382339f.8.1719443360576; Wed, 26 Jun 2024 16:09:20 -0700 (PDT) Precedence: bulk list-help: list-post: List-Id: internals.lists.php.net MIME-Version: 1.0 References: <6D0733B2-A1D1-45EC-B93E-029B1616A22F@miles.systems> <71dc8137-ded0-41dc-bc9b-32dde12f9170@app.fastmail.com> <3fb543ad-a497-43c4-952e-8842b4e11e27@app.fastmail.com> In-Reply-To: <3fb543ad-a497-43c4-952e-8842b4e11e27@app.fastmail.com> Date: Wed, 26 Jun 2024 17:09:09 -0600 Message-ID: Subject: Re: [PHP-DEV] [Initial Feedback] Typed Arrays To: Rob Landers Cc: internals@lists.php.net Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable From: levi.morrison@datadoghq.com (Levi Morrison) On Wed, Jun 26, 2024 at 3:27=E2=80=AFPM Rob Landers wro= te: > > > > On Wed, Jun 26, 2024, at 22:56, Casper Langemeijer wrote: > > > On Wed, Jun 26, 2024, at 21:59, Richard Miles wrote: > > I think we should have typed arrays in PHP. > > > Yes! I cannot stand sitting through conference talks on 'generics' that o= nly 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 docblocks to tell = my editor about the types. > > In my opinion, even if we would have some implementation of generics, hav= ing typed arrays with a simple syntax would be awesome. > > A syntax suggestion: > > $array =3D stdClass[]; > class A { > public stdClass[] $array; > } > > Adding an invalid array member should throw TypeError. > > I know 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 assi= gned as value of public A[] $a ?) > > Generics or bust. > > > I do not understand the reasoning behind that. Is it because we really wa= nt generics, but when the 95% use-case is solved we fear that there would n= ot be enough momentum to get that? I'd love to have generics too, but a ver= y 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, 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 like = 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. > > =E2=80=94 Rob I have seen variants of this several times. Many will hook into the autoloader and generate a class/interface definition on-the-fly to make it work.