Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix each* methods to return a receiver itself #3815

Closed

Conversation

makenowjust
Copy link
Contributor

@makenowjust makenowjust commented Jan 1, 2017

All each and methods like each should return a receiver itself. For example, current Deque#each returns Range?, it looks like a bug. (But each_cons and some Enumerable methods return nil same as Ruby)

@makenowjust makenowjust changed the title Fix each* methods to return self Fix each* methods to return a receiver itself Jan 2, 2017
@asterite
Copy link
Member

asterite commented Jan 2, 2017

Thank you!

We copied Ruby here when we made each return self. However, now I'm more inclined to make them return nil. I never understood why it's useful to return self. But I'd like others' opinion here :-)

@makenowjust
Copy link
Contributor Author

makenowjust commented Jan 2, 2017

@asterite I asked it to matz in twitter, and answered now. https://twitter.com/yukihiro_matz/status/815905904084197376

Matz says "To return self by Array#each and some methods is not big meaning, because returning value is not used almost. But it is better than to return nil."

@makenowjust
Copy link
Contributor Author

@asterite My question: To return self, does it have effects for optimization? And if returning nil, does it have no effect?

@asterite
Copy link
Member

asterite commented Jan 2, 2017

Yes, of course :-)

If a method returns nil then for code generation it returns void in LLVM. Then if you have a method like:

def foo
  some_array.each do
  end
end

then foo now returns the array, but if each returns nil then foo will return nil. This can also prevent some union types.

But this is not the point. I never understood why each (or p, or puts) return something other than nil. This is kind of ugly:

obj.each do
  # ...
end.do_something_else

so maybe I'd like to remove that anti-feature from the standard library.

@ysbaddaden
Copy link
Contributor

I think we can remove it. It's not such a big deal.

@asterite
Copy link
Member

asterite commented Jan 5, 2017

Closed in favor of #3826

@asterite asterite closed this Jan 5, 2017
@makenowjust makenowjust deleted the fix/each-return-self branch January 5, 2017 14:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants