Req #70364 [Com]: foreach type hinting
| From: | eliasvo at gmail dot com | Date: | Wed, 09 Sep 2015 11:08:46 +0000 |
| Subject: | Req #70364 [Com]: foreach type hinting | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-195903@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
Comment by: eliasvo at gmail dot com
Reported by: untuned20 at gmail dot com
Summary: foreach type hinting
Status: Open
Type: Feature/Change Request
Package: Scripting Engine problem
PHP Version: Irrelevant
Block user comment: N
Private report: N
New Comment:
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
});
Previous Comments:
------------------------------------------------------------------------
[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