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

Explicit topic variable _ disappears in a module's outermost scope #86

Closed
catb0t opened this issue Mar 9, 2019 · 5 comments
Closed

Comments

@catb0t
Copy link
Contributor

catb0t commented Mar 9, 2019

module X {
  [1, 2].each{
    say ( _ )      # unbound 
    { _ }          # unbound
  }
}
[INFO] `_` is parsed as a prefix method-call at no_topic.sm line 2
sidef: Okie dokie, I'm dead

File : no_topic.sm
Line : 2 : 8
Error: variable <_> is not declared in the current scope

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  say ( _ )
        ^
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Am I doing something wrong? The code works if you use |block_scope| binding in the each and the sub-block, it's only _ alone that isn't bound.

@catb0t
Copy link
Contributor Author

catb0t commented Mar 9, 2019

Thanks for the fix! And sorry for the unending barrage of bug reports, I do try to see if I can fix them myself first, but you know the code best.

@trizen
Copy link
Owner

trizen commented Mar 9, 2019

Thanks for the bug-reports. I admire your testing skills. No reason to be sorry. :)

@catb0t
Copy link
Contributor Author

catb0t commented Mar 10, 2019

With the simplifications I think you have introduced a regression, _ is unbound in the following, even inside a function or method (I can't remember if this was always the case)

var f = true
while (f) {
  assert(_)
  f = false
}

@trizen
Copy link
Owner

trizen commented Mar 10, 2019

This is intended: the if and while parameter must be explicitly defined, otherwise it would pollute the scope with a topic variable (_), most of the time unnecessarily.

var f = true
while (f) { |_|
  assert(_)
  f = false
}

This also allows the following code to work as intended (using _ from map inside if):

[1,2,3,4,5].map { if (_.is_even) { _/2 } else { 3*_ + 1 } }

@catb0t
Copy link
Contributor Author

catb0t commented Mar 10, 2019

Aha, right.

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

No branches or pull requests

2 participants