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

Update loop #5975

Closed
wants to merge 1 commit into from
Closed

Update loop #5975

wants to merge 1 commit into from

Conversation

Willamin
Copy link
Contributor

This adds an optional argument to the top level loop method. Sometimes it makes sense to start iterating at a different number than 0.

Copy link
Member

@oprypin oprypin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please make the commit summary more descriptive.

Note: I am not voting for or against this change, just checking implementation.

src/kernel.cr Outdated
@@ -25,8 +25,7 @@ ARGF = IO::ARGF.new(ARGV, STDIN)
# # ...
# end
# ```
def loop
i = 0
def loop(i = 0)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should have a better name, like start.
Could utilize external names

This adds an optional argument to the top level `loop` method, while continuing to default to `0`.
@asterite
Copy link
Member

Even though this is correct, maybe it's time to remove the argument to loop. It's not intuitive, it's Int32 by default, and it can overflow.

I'd vote to remove it.

@Willamin
Copy link
Contributor Author

If loop lacked the block argument, would you suggest people create their own counters?
Something like this?

my_counter = 0
loop do
  puts my_counter
end

@asterite
Copy link
Member

Yes, that's what you had to do before. Well, almost, you'd also increment the counter ;-)

@Willamin
Copy link
Contributor Author

Oh yes, updating the counter inside the loop 😄

@bararchy
Copy link
Contributor

I love the counter in loop, make it Int128 and were all OK :)

@Sija
Copy link
Contributor

Sija commented Apr 20, 2018

@asterite I'd rather add a NOTE regarding possible overflow, since it's rather common flow to use a counter inside of a loop. Also, couldn't we change it to UInt32/64?

@asterite
Copy link
Member

Imagine one day we raise on overflow. Then loop will always eventually raise. Doesn't sound good. Let's remove that block argument already :-)

@asterite
Copy link
Member

Again, we can add a note. Someone doesn't read it and they have a loop in a server:

loop do
  # accept connection
end

if we ever introduce raise on overflow, that will eventually raise. Why not make it explicit and use a counter, instead of loop having a variable? I can't remember who had the idea to introduce a counter... maybe me? If so, I regret it and I'd like to remove it.

@bcardiff
Copy link
Member

For iterating other indices Range can be used. I agree we can remove loop (and would love to see raise on overflow :-) )

@Willamin
Copy link
Contributor Author

Could we follow the example of each and each_with_index? Have a loop and a loop_with_index?

@Willamin
Copy link
Contributor Author

We can't iterate infinitely over a Range, though

@bcardiff
Copy link
Member

Ref #1407

@Willamin
Copy link
Contributor Author

I think it would be fine to keep the iteration, especially since it gets optimized out when unused. If we ever introduce a raise on overflow, it would be easy to provide backwards compatibility by having the definition of loop rescue the exception. However, because Crystal isn't 1.0.0, I don't think backwards compatibility is all that necessary.

@bararchy
Copy link
Contributor

Can we do

loop do 
 # no counter used
end
loop do |c|
  puts c
end

@Willamin
Copy link
Contributor Author

Willamin commented Apr 20, 2018

Yes, that's what happens currently. If you compile with the --release flag and don't use the counter, the counter gets optimized out.

@RX14
Copy link
Contributor

RX14 commented Apr 20, 2018

I can't really see any way to make this counter make sense after we add overflow detection, so I'm fine for removing it.

@straight-shoota
Copy link
Member

straight-shoota commented Apr 23, 2018

Without a counter, there is however little benefit for loop over while true... It's just the block scope.

@bararchy
Copy link
Contributor

@straight-shoota the block scope is handy, if you want work being done inside the loop but don't want the work to leak outside

@RX14
Copy link
Contributor

RX14 commented Apr 23, 2018

@straight-shoota I use loop do instead of while true because it's more descriptive. There don't need to be a concrete technical benefit, loop do is wirth it just for the naming.

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.

8 participants