Skip to content

Throw an exception when attempting to count a generator#1672

Closed
duncan3dc wants to merge 2 commits into
php:PHP-7.0from
duncan3dc:prevent-generator-count
Closed

Throw an exception when attempting to count a generator#1672
duncan3dc wants to merge 2 commits into
php:PHP-7.0from
duncan3dc:prevent-generator-count

Conversation

@duncan3dc

Copy link
Copy Markdown
Member

Generators are often looped around, and where you have code that accepts a traversable, it's logical to try and count it. At the moment counting a generator returns int(1), so the unsupported behaviour can easily go unnoticed.

This pr proposes to change that by throwing an exception whenever code attempts to count a generator

@duncan3dc duncan3dc force-pushed the prevent-generator-count branch from 993a234 to bec53f1 Compare December 15, 2015 15:13
@duncan3dc duncan3dc force-pushed the prevent-generator-count branch from bec53f1 to 9895754 Compare December 17, 2015 19:58
@jpauli

jpauli commented Dec 24, 2015

Copy link
Copy Markdown
Member

This can't target PHP 7.0 as it breaks API.
It can target PHP 7.1 though.
ping @nikic ?

@nikic

nikic commented Dec 24, 2015

Copy link
Copy Markdown
Member

@jpauli I think this is a good change.

However I wonder whether we shouldn't make this an error for all counts on non-Countable objects. The current behavior is very useless and probably easy to make a mistake if you're working with iterators.

@duncan3dc

duncan3dc commented Dec 24, 2015 via email

Copy link
Copy Markdown
Member Author

@jpauli

jpauli commented Jan 4, 2016

Copy link
Copy Markdown
Member

@nikic I'm also +1 to throw an Exception on any count operation involving non-countable object , but that should target 8.0

@duncan3dc I think having a patch just for the Generator case makes little sense if we choose to apply it to every object. I guess it is time to propose such a change into an RFC

@nikic

nikic commented Jan 4, 2016

Copy link
Copy Markdown
Member

An RFC should be able to target this at 7.1. Whether it will pass likely depends on whether someone finds a legit BC concern with the change.

@jpauli

jpauli commented Jan 4, 2016

Copy link
Copy Markdown
Member

I think the BC is too big for a minor and should target a major

@jerrygrey

Copy link
Copy Markdown

Throw an Exception for all non-countable objects is a good idea, I'm surprised it doesn't do it already.

@morrisonlevi

Copy link
Copy Markdown
Contributor

Something that is related: foreach on objects that do not implement Iterator will iterate over the visible properties. It is my opinion that these should be tackled in connection with each other.

@nikic

nikic commented Jan 29, 2016

Copy link
Copy Markdown
Member

@jpauli How about a warning? Exceptions in standard library are a tricky issue, but I don't think anybody would have a problem with a warning.

@morrisonlevi That seems unrelated, and the current behavior is an explicitly supported feature. I'd appreciate the introduction of a proper property iterator type, which would allow us to phase out this behavior in the future, however I don't see the relation to count().

@jpauli

jpauli commented Jan 29, 2016

Copy link
Copy Markdown
Member

@nikic Why not, but this would BC as well :-p

@duncan3dc

Copy link
Copy Markdown
Member Author

@jpauli What about E_DEPRECATED for 7.*?

@nikic

nikic commented Jan 29, 2016

Copy link
Copy Markdown
Member

@jpauli I don't think we consider throwing additional non-fatal diagnostics to be a BC break. We really should specify this kind of stuff.

@jpauli

jpauli commented Jan 29, 2016

Copy link
Copy Markdown
Member

To be asked to the RM :-)

@morrisonlevi

Copy link
Copy Markdown
Contributor

In any case throwing an exception or raising an error should not be specific to Generators in my opinion. It's the same exact logic issue with all other non-Countable objects. I think we should not special case the Generator.

Said otherwise we should raise an error/warning/notice for all non-Countable objects (not just Generators)

@jpauli

jpauli commented Jan 29, 2016

Copy link
Copy Markdown
Member

@morrisonlevi I tend to agree here, though I'm still not sure it should be done in a minor

@duncan3dc

Copy link
Copy Markdown
Member Author

Thanks for the input everyone, the RFC is up for discussion now

@duncan3dc duncan3dc closed this Oct 4, 2016
@duncan3dc duncan3dc deleted the prevent-generator-count branch October 4, 2016 20:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants