Skip to content

class Crystal::MatchContext
inherits Reference

Context information about a method lookup match.

For example, given:

class Foo
  class Baz
  end

  def method
    Baz.new
  end
end

class Bar < Foo
  class Baz
  end
end

Bar.new.method # => #<Foo::Baz:0x10c1a2fc0>

we have that:

  • defining_type is Foo, because it's the type that define the method
  • instantiated_type is Bar, because the method was invoked, and thus instantiated there

defining_type is needed because when we search types in method, we must search them starting from Foo, not from Bar. instantiated_type is needed because method resolution will start from Bar.

Todo

this might slightly change in the future, we should probably instantiate the method on Foo+ to avoid having duplicated methods, at least for reference types, even though that might lead to broader method resolutions (because a method call in method will now be searched in Foo+, not in Bar)

Class methods

.new(instantiated_type, defining_type, free_vars = nil, strict = false, def_free_vars = nil)

View source

Methods

#clone

View source

#def_free_vars : Array(String)?

Def free variables, unbound (def (X, Y) ...)

View source

#def_free_vars=(def_free_vars : Array(String)?)

Def free variables, unbound (def (X, Y) ...)

View source

#defining_type : Type

The type that defines the method

View source

#defining_type=(defining_type : Type)

The type that defines the method

View source

#free_vars : Hash(String, TypeVar)?

Any instance variables associated with the method instantiation

View source

#get_free_var(name)

View source

#has_def_free_var?(name)

View source

#instantiated_type : Type

The type where the method was instantiated

View source

#instantiated_type=(instantiated_type : Type)

The type where the method was instantiated

View source

#self_type : Type

Returns the type that corresponds to using self when looking a type relative to this context.

For example, given:

class Foo
  def foo(&block : self ->)
    ...
  end
end

class Bar < Foo
end

Bar.new.foo { |x| }

it's expected that the block argument x will be of type Bar, not Foo.

View source

#set_free_var(name, type)

View source

#strict? : Bool

View source