Skip to content

class Crystal::InstanceVarsInitializerVisitor
inherits Crystal::SemanticVisitor

In this pass we check instance var initializers like:

@x = 1
@x : Int32 = 1

These initializers run when an instance is created, so there's no way that the main code can use them before creating an instance to use them. Conclusion: we must analyze them before the main code.

This solves the following problem:

# Here the compiler would complain because @bar
# wasn't initialized/analyzed yet
Foo.new.bar + 1

class Foo
  @bar = 1

  def bar
    @bar
  end
end

Methods

#finish

View source

#type_instance_var(node, target, value)

View source

#visit(node : Assign)

View source

#visit(node : TypeDeclaration)

View source

#visit_any(node)

View source