Class: Steep::Subtyping::VariableOccurence

Inherits:
Object
  • Object
show all
Defined in:
lib/steep/subtyping/variable_occurrence.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeVariableOccurence

Returns a new instance of VariableOccurence.



7
8
9
10
# File 'lib/steep/subtyping/variable_occurrence.rb', line 7

def initialize
  @params = Set.new
  @returns = Set.new
end

Instance Attribute Details

#paramsObject (readonly)

Returns the value of attribute params.



4
5
6
# File 'lib/steep/subtyping/variable_occurrence.rb', line 4

def params
  @params
end

#returnsObject (readonly)

Returns the value of attribute returns.



5
6
7
# File 'lib/steep/subtyping/variable_occurrence.rb', line 5

def returns
  @returns
end

Class Method Details

.from_method_type(method_type) ⇒ Object



42
43
44
45
46
# File 'lib/steep/subtyping/variable_occurrence.rb', line 42

def self.from_method_type(method_type)
  self.new.tap do |occurence|
    occurence.add_method_type(method_type)
  end
end

Instance Method Details

#add_method_type(method_type) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/steep/subtyping/variable_occurrence.rb', line 12

def add_method_type(method_type)
  method_type.params.each_type do |type|
    each_var(type) do |var|
      params << var
    end
  end
  each_var(method_type.return_type) do |var|
    returns << var
  end

  method_type.block&.yield_self do |block|
    block.type.params.each_type do |type|
      each_var(type) do |var|
        params << var
      end
    end
    each_var(block.type.return_type) do |var|
      returns << var
    end
  end
end

#each_var(type, &block) ⇒ Object



34
35
36
# File 'lib/steep/subtyping/variable_occurrence.rb', line 34

def each_var(type, &block)
  type.free_variables.each(&block)
end

#strictly_return?(var) ⇒ Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/steep/subtyping/variable_occurrence.rb', line 38

def strictly_return?(var)
  !params.member?(var) && returns.member?(var)
end