Class: Steep::AST::Types::Intersection

Inherits:
Object
  • Object
show all
Includes:
Helper::ChildrenLevel
Defined in:
lib/steep/ast/types/intersection.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Helper::ChildrenLevel

#level_of_children

Constructor Details

#initialize(types:, location: nil) ⇒ Intersection

Returns a new instance of Intersection.



8
9
10
11
# File 'lib/steep/ast/types/intersection.rb', line 8

def initialize(types:, location: nil)
  @types = types
  @location = location
end

Instance Attribute Details

#locationObject (readonly)

Returns the value of attribute location.



6
7
8
# File 'lib/steep/ast/types/intersection.rb', line 6

def location
  @location
end

#typesObject (readonly)

Returns the value of attribute types.



5
6
7
# File 'lib/steep/ast/types/intersection.rb', line 5

def types
  @types
end

Class Method Details

.build(types:, location: nil) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/steep/ast/types/intersection.rb', line 13

def self.build(types:, location: nil)
  types.flat_map do |type|
    if type.is_a?(Intersection)
      type.types
    else
      [type]
    end
  end.map do |type|
    case type
    when AST::Types::Any
      return AST::Types::Any.new(location: location)
    when AST::Types::Bot
      return AST::Types::Bot.new(location: location)
    when AST::Types::Top
      nil
    else
      type
    end
  end.compact.uniq.yield_self do |tys|
    if tys.size == 1
      tys.first
    else
      new(types: tys.sort_by(&:hash), location: location)
    end
  end
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



40
41
42
43
# File 'lib/steep/ast/types/intersection.rb', line 40

def ==(other)
  other.is_a?(Intersection) &&
    other.types == types
end

#free_variablesObject



60
61
62
63
64
# File 'lib/steep/ast/types/intersection.rb', line 60

def free_variables
  types.each.with_object(Set.new) do |type, set|
    set.merge(type.free_variables)
  end
end

#hashObject



45
46
47
# File 'lib/steep/ast/types/intersection.rb', line 45

def hash
  self.class.hash ^ types.hash
end

#levelObject



68
69
70
# File 'lib/steep/ast/types/intersection.rb', line 68

def level
  [0] + level_of_children(types)
end

#subst(s) ⇒ Object



51
52
53
54
# File 'lib/steep/ast/types/intersection.rb', line 51

def subst(s)
  self.class.build(location: location,
                   types: types.map {|ty| ty.subst(s) })
end

#to_sObject



56
57
58
# File 'lib/steep/ast/types/intersection.rb', line 56

def to_s
  "(#{types.map(&:to_s).sort.join(" & ")})"
end