Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:108188 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 53963 invoked from network); 17 Jan 2020 21:09:33 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 17 Jan 2020 21:09:33 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 04704180549 for ; Fri, 17 Jan 2020 11:17:16 -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,DKIM_SIGNED, DKIM_VALID,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_NONE autolearn=no autolearn_force=no version=3.4.2 X-Spam-ASN: AS15169 209.85.128.0/17 X-Spam-Virus: No X-Envelope-From: Received: from mail-yb1-f175.google.com (mail-yb1-f175.google.com [209.85.219.175]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Fri, 17 Jan 2020 11:17:15 -0800 (PST) Received: by mail-yb1-f175.google.com with SMTP id k128so6620110ybc.13 for ; Fri, 17 Jan 2020 11:17:15 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=newclarity-net.20150623.gappssmtp.com; s=20150623; h=mime-version:subject:from:in-reply-to:date:cc :content-transfer-encoding:message-id:references:to; bh=JdVrKZLxJqMuHnraSPXsiZWPBafN+G1ihxZHE2DoJR4=; b=OA5o4ueQzlssoASkdx0X/5mUSslqg6XuhEGDnSN6Lf0b/RJ/i4RinPXnq/zCoFigCI 0doXILHbDhlS6jpd03sbr1QDsBSiq0U9cW09fiBhmFz+AFkl/ciTlzzBnWt2vfqx4uE6 4n5JsMwsTVaD1/kC2vXF9Vpq2uQ7+49hTG6wGqeqES2OjzBDqRQL9HCC0JEIpvZkc8hi VBcRkg/iih3DlAvKjR2oiSvsNQUDuWKdneSUYWJv8FY3C+wRVHO16DAlPMdKjoKNQxw3 kLOvxwGkBAM3q3SJoBtAX17w80+PgkA9yy9QssWa1B6+0HLHMltZK/lvAxhep/+ODgp6 +EqA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:subject:from:in-reply-to:date:cc :content-transfer-encoding:message-id:references:to; bh=JdVrKZLxJqMuHnraSPXsiZWPBafN+G1ihxZHE2DoJR4=; b=U3byjPZ8LNaxS41S9eNM3L2tVHS0oQMxbuvWvw564hGfR6oCS6nngwa9iAhKd63/Fu LfOVN+P6Q0dEtnOYrX9lyMdCGtO9S3C+dcMwE1my3yd8DEHUf5km04uH8q9iEsSC7GCq 79u+0srmUtiieey63IQWEIuVOKJv452+NOgYEA6+44BMJYEm4b07KsbaxgLMIzOWRM3E D+UseBw0koTpaG+0k846GKV6bZ2VIQQgY9tp1rOu5tO6phM3WQBBQCiIfU/ynRLdzGpB ZRUm2DQ1LVKsdJMkGrD3DFfXCeqX+M7pLIDh/bW57LhRBSmNhkh1FiC6YTPNRAn2kyPK lzvA== X-Gm-Message-State: APjAAAUND37qwpuM+yok0ryXeBQucSVBs5E50xrb1YiUfvax0uCgZQVE r4NOkeA/aTJ2E7UFF9pt7KuzBg== X-Google-Smtp-Source: APXvYqyHDk4l03UXmGdbOqg6RfzzI945GNusEZ2gCF4EJZzyr4WBNOHP+dKjdhurmuTcvvrJPKncxw== X-Received: by 2002:a25:3604:: with SMTP id d4mr28549015yba.290.1579288634715; Fri, 17 Jan 2020 11:17:14 -0800 (PST) Received: from ?IPv6:2601:c0:c680:5cc0:8da9:6c47:ef69:1ba8? ([2601:c0:c680:5cc0:8da9:6c47:ef69:1ba8]) by smtp.gmail.com with ESMTPSA id i127sm11431408ywe.65.2020.01.17.11.17.12 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 17 Jan 2020 11:17:12 -0800 (PST) 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 14:17:11 -0500 Cc: internals@lists.php.net 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: mike@newclarity.net (Mike Schinkel) > On Jan 17, 2020, at 2:50 AM, Aran Reeks wrote: > 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. I for one would be a big +1 for this, with caveats. =20 > IDEs like PHPStorm handle this structure already hence sticking to = that as > a starting point... >=20 > @returns []int As previously noted, I assume you meant int[]? ---- Having the ability to type array elements would cover ~90% of the cases = where I cannot properly type parameters or return values in PHP 7.4. The caveat is that it would seem that to check dynamically would be an = expensive proposition such as when an array that was not typed is passed = to or returned from a function or assigned to a variable of a declared = type, e.g. function foo( $myarray ): Foo[] { return $myarray; // This would need to be dynamically checked, = I think? } Of course we could limit this type of typing to only arrays that are = already know to be typed, meaning this would always fail: function bar( Foo[] $myarray ) { // Do whatever } function baz( $myarray ) { bar( $myarray ); // Fails here because $myarray not known to be = Foo[] even when it is } baz( [ new Foo() ] ); Alternately we could have a global option that would do type checking or = not for these type hints, so they could be dynamically checked for all = code prior to production code, where checking could be turned off. Another option could be if the number of array elements is small (<100?) = it could check, but otherwise not check, but this feels all kind of = different types of wrong. ---- My vote, if I had one, would be to add type new typing for array = elements, but also add a type checking global option that can be in one = of 3 states:=20 1. Static checks only,=20 2. Dynamic checks only, or=20 3. No checking of array elements. #jmtcw -Mike P.S. Or maybe there is an inexpensive way to keep track of the types of = the entire array on element assignment? P.P.S. Can someone please explain and give an example of how generics = would make this need moot? I do not get why that would be the case...