diff options
author | Nikita Popov <nikic@php.net> | 2012-05-30 02:44:06 +0200 |
---|---|---|
committer | Nikita Popov <nikic@php.net> | 2012-05-30 02:44:06 +0200 |
commit | bc08c2cf9485e20fea0eef7ab149cefdf9a3662e (patch) | |
tree | 902abf875287ba8842600cc5e84557ee30bfc2f4 /Zend/tests/generators | |
parent | 72a91d08e7d70d5524feb6cc7c8e32b3bd68f1df (diff) | |
download | php-git-bc08c2cf9485e20fea0eef7ab149cefdf9a3662e.tar.gz |
Add support for yielding keys
Keys are yielded using the
yield $key => $value
syntax. Currently this is implemented as a statement only and not as an
expression, because conflicts arise considering nesting and use in arrays:
yield yield $a => $b;
// could be either
yield (yield $a) => $b;
// or
yield (yield $a => $b);
Once I find some way to resolve these conflicts this should be available
as an expression too.
Also the key yielding code is rather copy-and-past-y for the value yielding
code, so that should be factored out.
Diffstat (limited to 'Zend/tests/generators')
-rw-r--r-- | Zend/tests/generators/generator_with_keys.phpt | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/Zend/tests/generators/generator_with_keys.phpt b/Zend/tests/generators/generator_with_keys.phpt new file mode 100644 index 0000000000..43e3e5ecf7 --- /dev/null +++ b/Zend/tests/generators/generator_with_keys.phpt @@ -0,0 +1,26 @@ +--TEST-- +Generators can also yield keys +--FILE-- +<?php + +function *reverse(array $array) { + end($array); + while (null !== $key = key($array)) { + yield $key => current($array); + prev($array); + } +} + +$array = [ + 'foo' => 'bar', + 'bar' => 'foo', +]; + +foreach (reverse($array) as $key => $value) { + echo $key, ' => ', $value, "\n"; +} + +?> +--EXPECT-- +bar => foo +foo => bar |