crystal icon indicating copy to clipboard operation
crystal copied to clipboard

Multiple ivar assignment considered 'use' rather than 'setting'

Open docelic opened this issue 4 years ago • 1 comments

Here's an issue I observed on 0.35.1. Setting instance variables works when done one variable at a time, but not with multiple ivars/values at once:

class X
  @x : Int32
  @y : Int32
  
  # This part is OK
  def initialize
    @x, @y = find_out
  end
 
  def find_out
    x = 12
    y = 14
    
    # This works:
    #@x = x
    #@y = y
    
    # But this doesn't:
    @x, @y = x, y
    
    {x,y}
  end
end
 
X.new

https://carc.in/#/r/a5z5

(On a side note, the reason why the values are both assigned and returned is because I want the method to set the values. The return and setting the values in initialize, one-time, is to get around the indirect initialization not supported.)

docelic avatar Dec 24 '20 15:12 docelic

For reference, the issue is still present on 1.13.1:

crystal  test2.cr 
Showing last frame. Use --error-trace for full trace.

Error: instance variable '@x' of X must be Int32, not Nil

Instance variable '@x' was used before it was initialized in one of the 'initialize' methods, rendering it nilable

docelic avatar Jul 28 '24 07:07 docelic