On Sat, Aug 18, 2012 at 4:12 PM,  <les...@lsces.co.uk> wrote:
> Since this is yet another area where 'one does not have to use it if one
> does not want to' ... FLAGGING to the other users that a function is a
> 'special one' rather than just a normal function with a generator function
> seems to me just a necessity? HAVING to work through a functions code
> simply to see if it contains a 'yeild' so as to understand that it's not
> going to give a normal return seems insane? Alright the function can be
> properly documented with a docblock, but if it isn't ...

I don't understand this argument. Generator functions are transparent
to the user. You use a generator function just like you would use a
function that returns an array. From a user point of view it does not
matter whether getLinesFromFile() is just a function returning an
array, or whether it is a generator (or maybe even returns a
hand-implemented iterator). It's just all the same. The fact that the
function uses `yield` internally is just an implementation detail, not
something that has to be part of the public API. Actually you can swap
between an array and generator implementation just by replacing one
line in the function body (yield <=> $array[])...

>>> generator function getLinesFromFile($fileName) {
>>>     if (!$fileHandle = fopen($fileName, 'r')) {
>>>        return;
>>>    }
>>
>>>     There is an existing generator implementation in HipHop PHP, which
>>> uses automatic-detection. Using the asterix modifier would break
>>> compatibility.
>>
>> This should not be a concern, sure, it's annoying for the hiphop
>> developers but they chose to copy and then *chance* the PHP language for
>> their own effect.
>>
>>> yield: Yields the value null with an auto-incrementing integer key.
>> What is the usecase for this?
> I can see some interesting porting problems with this ... one of the stock
> changes when moving from a MySQL only setup to support other databases is
> to remove the auto-increment magic, and replace it with proper sequence
> code prior to inserting. I can see translating a 'MySQL' geared generator
> into a more general one as being impossible if some tricks like this are
> used.

Generators are supposed to act very similar to array-returning
functions, so they also have similar key semantics. If you do
$result[] = $foo; that will behave the same as doing yield $foo;. Both
will use an auto-incrementing key.

Nikita

-- 
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to