Req #70364 [Opn->Sus]: foreach type hinting
| From: | stas@php.net | Date: | Sat, 05 Aug 2017 04:48:31 +0000 |
| Subject: | Req #70364 [Opn->Sus]: foreach type hinting | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-210512@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=70364&edit=1
ID: 70364
Updated by: stas@php.net
Reported by: untuned20 at gmail dot com
Summary: foreach type hinting
-Status: Open
+Status: Suspended
Type: Feature/Change Request
Package: Scripting Engine problem
PHP Version: Irrelevant
Block user comment: N
Private report: N
New Comment:
Thank you for your interest in PHP and for submitting a feature request. Please be aware that due to
the magnitude of change this request requires, it would be necessary to discuss it on PHP Internals
list (internals@lists.php.net) as an RFC. Please read the guide about creating RFCs here:
https://wiki.php.net/rfc/howto
If you haven't had experience with writing RFCs before, it is advised to seek guidance on the
Internals list (http://php.net/mailing-lists.php) and/or solicit help from one of the experienced
developers.
Please to not consider this comment as a negative view on the merits of your proposal - every
proposal which requires changes of certain magnitude, even the very successful and widely supported
ones, must be done through the RFC process. This helps make the process predictable, transparent and
accessible to all developers.
Previous Comments:
------------------------------------------------------------------------
[2015-09-09 11:08:43] eliasvo at gmail dot com
I, for one, don't see the need for typed language constructs (heck, even languages like C++
have introduced the auto keyword because iterating over complex types was getting way to verbose).
However, if this is something that is going to be implemented, there should be more thought put into
this. Things to consider:
- What is the expected behaviour if the type constraint is not met (catchable fatal, exception,
warning, notice, ...?)
- What about allowing multiple types? Do we need to introduce the vague mixed type-hint?
- Scalars... do we implement type-hints for the keys, too (foreach ($array as int $key => string
$value))
- If key type-hints are made possible, what do they do: the int $key hint should only allow numeric
indexes or b) should skip over any key that is not numeric c) convert numeric strings to ints <-
should this change the hashtable we're iterating over (probably not), but it might cause issues
later on if it doesn't
Tl;tr
This request needs a lot more thought put into it, but really, there's no need as there are
plenty of other ways to achieve this already:
array_map($instanceList, function (MyClass $instance) {
//element is guaranteed to be instance of MyClass here
});
------------------------------------------------------------------------
[2015-09-03 01:52:03] untuned20 at gmail dot com
I am seeing that php is trying to get more and more into type hinting, and that it would be nice to
have type hinting in foreach loops as well as in functions. Not only would it give the same
functionality as when declaring a function, but it would also allow IDE's to autocomplete
nicely.
------------------------------------------------------------------------
[2015-09-03 01:37:28] yohgaki@php.net
@untuned20
Do you have problem with
foreach($classList as $class) {
assert($class instanceof MyClass);
}
or something like this?
------------------------------------------------------------------------
[2015-08-26 16:21:23] untuned20 at gmail dot com
Description:
------------
Allow for the ability to type hint a foreach loop.
Test script:
---------------
<?php
$classList = [/* List of classes */];
foreach($classList as $index => MyClass $class){}
foreach($classList as MyClass $class){}
class MyClass{
/* Methods and Properties */
}
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=70364&edit=1