Class: Steep::AST::Types::Union

Inherits:
Object
  • Object
show all
Includes:
Helper::ChildrenLevel
Defined in:
lib/steep/ast/types/union.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:) ⇒ Union

Returns a new instance of Union.



7
8
9
# File 'lib/steep/ast/types/union.rb', line 7

def initialize(types:)
  @types = types
end

Instance Attribute Details

#typesObject (readonly)

Returns the value of attribute types.



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

def types
  @types
end

Class Method Details

.build(types:) ⇒ Object



11
12
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
39
40
41
42
43
44
# File 'lib/steep/ast/types/union.rb', line 11

def self.build(types:)
  return AST::Types::Bot.instance if types.empty?
  if types.size == 1
    return types.first || raise
  end

  types.flat_map do |type|
    if type.is_a?(Union)
      type.types
    else
      [type]
    end
  end.map do |type|
    case type
    when AST::Types::Any
      return AST::Types::Any.instance()
    when AST::Types::Top
      return AST::Types::Top.instance
    when AST::Types::Bot
      nil
    else
      type
    end
  end.compact.uniq.yield_self do |tys|
    case tys.size
    when 0
      AST::Types::Bot.instance
    when 1
      tys.first || raise
    else
      new(types: tys)
    end
  end
end

Instance Method Details

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



46
47
48
49
# File 'lib/steep/ast/types/union.rb', line 46

def ==(other)
  other.is_a?(Union) &&
    Set.new(other.types) == Set.new(types)
end

#each_child(&block) ⇒ Object



73
74
75
76
77
78
79
# File 'lib/steep/ast/types/union.rb', line 73

def each_child(&block)
  if block
    types.each(&block)
  else
    types.each
  end
end

#free_variablesObject



65
66
67
68
69
70
71
# File 'lib/steep/ast/types/union.rb', line 65

def free_variables
  @fvs ||= Set.new.tap do |set|
    types.each do |type|
      set.merge(type.free_variables)
    end
  end
end

#hashObject



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

def hash
  @hash ||= types.inject(self.class.hash) {|c, type| type.hash ^ c } #$ Integer
end

#levelObject



87
88
89
# File 'lib/steep/ast/types/union.rb', line 87

def level
  [0] + level_of_children(types)
end

#map_type(&block) ⇒ Object



81
82
83
# File 'lib/steep/ast/types/union.rb', line 81

def map_type(&block)
  Union.build(types: types.map(&block))
end

#subst(s) ⇒ Object



57
58
59
# File 'lib/steep/ast/types/union.rb', line 57

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

#to_sObject



61
62
63
# File 'lib/steep/ast/types/union.rb', line 61

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

#with_location(new_location) ⇒ Object



91
92
93
# File 'lib/steep/ast/types/union.rb', line 91

def with_location(new_location)
  self.class.new(types: types)
end