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

Assignment to variables inside array literals causes "read before definition of" #3195

Closed
oprypin opened this issue Aug 26, 2016 · 3 comments · Fixed by #10009
Closed

Assignment to variables inside array literals causes "read before definition of" #3195

oprypin opened this issue Aug 26, 2016 · 3 comments · Fixed by #10009
Labels
kind:bug A bug in the code. Does not apply to documentation, specs, etc. topic:compiler

Comments

@oprypin
Copy link
Member

oprypin commented Aug 26, 2016

def fa
  {a = 1}
  p a  # works
end; fa

def fb
  {2 => b = 2}
  p b  # works
end; fb

def fc
  [c = 3]
  p c
end; fc
Error in line 14: instantiating 'fc()'

in line 13: read before definition of 'c'
@jhass jhass added kind:bug A bug in the code. Does not apply to documentation, specs, etc. topic:compiler labels Aug 29, 2016
@asterite
Copy link
Member

asterite commented Sep 1, 2016

I'm not sure how to fix it, and I'd probably prefer to give an error if an assignment is used inside a literal (it's confusing)

@oprypin
Copy link
Member Author

oprypin commented Sep 1, 2016

I don't see why literals should be special cased like this. I'd hate to see this error out.

Nothing wrong with this:

objs = [(a = Obj.new("a")), (b = Obj.new("b"))]

@mverzilli
Copy link

That looks quite ugly. Disclaimer: I'm biased against embedding assignments in other expressions unless extremely necessary.

a, b = objs = [1, 2]
pp a # => 1
pp b # => 2
pp objs # => [1, 2]

Isn't this much more clear? (And it works! :))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind:bug A bug in the code. Does not apply to documentation, specs, etc. topic:compiler
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants