Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:108177 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 77166 invoked from network); 17 Jan 2020 10:09:15 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 17 Jan 2020 10:09:15 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 08C3B18050B for ; Fri, 17 Jan 2020 00:16:45 -0800 (PST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,SPF_HELO_NONE, SPF_PASS autolearn=no autolearn_force=no version=3.4.2 X-Spam-ASN: AS24940 159.69.0.0/16 X-Spam-Virus: No X-Envelope-From: Received: from srv015.mail.ichtushosting.com (srv015.mail.ichtushosting.com [159.69.182.195]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Fri, 17 Jan 2020 00:16:42 -0800 (PST) Received: from srv020.mail.ichtushosting.com ([78.46.213.219]) by srv015.mail.ichtushosting.com stage1 with esmtp (Exim MailCleaner) id 1isMo4-0002Rm-W8 from ; Fri, 17 Jan 2020 09:16:41 +0100 Received: from ptr-1ewoyg4acu9xtly9l2j.18120a2.ip6.access.telenet.be (ptr-1ewoyg4acu9xtly9l2j.18120a2.ip6.access.telenet.be [IPv6:2a02:1810:2f21:1400:7171:f0b1:9729:b8eb]) (Authenticated sender: brendt@stitcher.io) by srv020.mail.ichtushosting.com (Postfix) with ESMTPSA id B40763EC27; Fri, 17 Jan 2020 09:16:40 +0100 (CET) X-MailCleaner-SPF: pass Content-Type: text/plain; charset=us-ascii Mime-Version: 1.0 (Mac OS X Mail 12.4 \(3445.104.11\)) In-Reply-To: Date: Fri, 17 Jan 2020 09:16:39 +0100 Cc: Levi Morrison via internals Content-Transfer-Encoding: quoted-printable Message-ID: References: To: Aran Reeks X-Mailer: Apple Mail (2.3445.104.11) Subject: Re: [PHP-DEV] Typed array properties V2 From: brendt@stitcher.io (Brent Roose) Hello all It's a much-requested feature for years and years. My first thought was = "we need generics, not this" but than I took 5 minutes to actually think = about it. While the same, and much more, can be achieved with generics, = it's a difficult feature to implement. There have been several RFCs for = generics in the past, which failed. I know Levi Morisson was, at one = point, looking at adding support for generics only in traits, because = it's difficult to add them in other places. While I still think generics would be a great feature, I now also = believe it's worth looking at an "array of" type as something = standalone. I've got no clue about the technical implications, but maybe = suprting "array of" syntax is a lot more easy than full blown generics? = Looking at my day to day work with PHP, I'dsay "array of" types would = solve ~80% of my problems with PHP's current type system, and I figure = there are lots of developers in a similar situation. If I remember = correct from my college days, Java also supports both styles: Int[] and = ArrayList. All that to say that maybe it's worth the effort looking at "array of" = types as something different than generics? Kind regards Brent > On 17 Jan 2020, at 08:50, Aran Reeks wrote: >=20 > Hi Internals, >=20 > I'd like to kick off a conversation to capture everyone else's = thoughts on > tweaking / improving typed properties for arrays (for a PHP 8.x = release). >=20 > With all the work done lately to greatly improve the type support in = PHP > (which is amazing by the way), I'm finding for the most part, I'm no = longer > needing to Docblock as much of my code which is lovely. >=20 > That said, there's a common use case that keeps me going back to them = which > I think would be a good thing for PHP to try and solve as a language > feature - better typing of arrays to type their properties. >=20 > IDEs like PHPStorm handle this structure already hence sticking to = that as > a starting point... >=20 > @returns []int >=20 > This would designate the return of an array where all its keys are = that of > the int type, but it works for any type. >=20 > With that in mind, it might also make sense to allow a shorthand array > alias for array types anyway - array -> []. >=20 > To use actual PHP examples, this would mean the following would be > supported: >=20 > // Typed array properties ...values would follow any existing PHO type > function returnsIntArray(): []int; > function returnsClassArray(): []Class; >=20 > // The same outcome > function returnsArray(): array; > function returnsArray(): []; >=20 > I welcome all your thoughts on this proposal. >=20 > Many thanks, > Aran