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

Lexically scoped variables in the macro language #11140

Open
HertzDevil opened this issue Aug 26, 2021 · 1 comment
Open

Lexically scoped variables in the macro language #11140

HertzDevil opened this issue Aug 26, 2021 · 1 comment

Comments

@HertzDevil
Copy link
Contributor

The macro interpreter currently does not create lexical scopes for any block parameters. It is up to each block-accepting macro method to define the block variables when a call is interpreted, and these macro methods all leave the variables untouched after that call finishes:

{%
  x = 1
  [2].each { |x| }
  x # => 2
%}

This is inconsistent with blocks in normal code which do create a lexical scope. On the other hand, macro for loops destroy the variables upon exit:

{% begin %}
  {% x = 1 %}
  {% for x in [2] %}
  {% end %}
  {% x %} # Error: undefined macro variable 'x'
{% end %}

for loops don't exist in normal Crystal code, but in Ruby they retain the last values of the variables (and it's a great source of confusion there). IMO both macro constructs should support lexical scoping and the local shadowing of variable names.

@Blacksmoke16
Copy link
Member

+1 this has bitten me numerous times.

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

No branches or pull requests

2 participants