Conditional code shadows __LINE__

Given the following code:

{% if flag?(:DBG) %}
  def foo(x, linenum = 0)
    puts "x=#{x} : linenum=#{linenum}"
  end

  def bar
    foo("abc", linenum: __LINE__)
    foo("def", linenum: __LINE__)
    foo("ghi", linenum: __LINE__)
  end
{% else %}
  def foo(x)
    puts "x=#{x}"
  end

  def bar
    foo("abc")
    foo("def")
    foo("ghi")
  end
{% end %}

bar

Running crystal run foobar.cr -D DBG outputs:

x=abc : linenum=1
x=def : linenum=1
x=ghi : linenum=1  

Is this expected behaviour ?

I would have expected :

x=abc : linenum=7
x=def : linenum=8
x=ghi : linenum=9  
1 Like

Yes, {{ __LINE__ }} inside the macro should work the way you want

Thanks @HertzDevil
Of course, it’s obvious now!