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

break (inside while) accepts a value, but does nothing with it #1277

Closed
oprypin opened this issue Aug 25, 2015 · 3 comments · Fixed by #10566
Closed

break (inside while) accepts a value, but does nothing with it #1277

oprypin opened this issue Aug 25, 2015 · 3 comments · Fixed by #10566

Comments

@oprypin
Copy link
Member

oprypin commented Aug 25, 2015

x = while true
  break ":)"
end
p x
$ ruby test.cr
":)"
$ crystal test.cr
nil

I'm not a Ruby programmer, but my expectations matched with Ruby's behavior.

If it's decided that the while loop shouldn't have a return value in this case, then break should not allow arguments.

@asterite
Copy link
Member

Oh, I had no idea that the break inside a while gave it it's value. I always though it was nil.

I guess we can do this, but unless the condition is true and you break unconditionally, x will also get the type Nil (I think)

@yxhuvud
Copy link
Contributor

yxhuvud commented Feb 2, 2017

I guess we can do this, but unless the condition is true and you break unconditionally, x will also get the type Nil (I think)

You could let it return the last statement inside the loop. Does that have any downside?

@david50407
Copy link
Contributor

@yxhuvud default behavior with resulting nil is good for now, if we want to break with last value we can do

while x
  break something
end

it's more clearly than

while x
  something
  break # no notice for returning the 'something'
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants