Class: Yoda::Typing::Types::Union

Inherits:
Base
  • Object
show all
Defined in:
lib/yoda/typing/types/union.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#reference?

Constructor Details

#initialize(*types) ⇒ Union

Returns a new instance of Union.

Parameters:

  • types (Array<Base>)


23
24
25
26
# File 'lib/yoda/typing/types/union.rb', line 23

def initialize(*types)
  types.each { |type| fail TypeError, type unless type.is_a?(Types::Base) }
  @types = types
end

Instance Attribute Details

#typesArray<Base> (readonly)

Returns:



6
7
8
# File 'lib/yoda/typing/types/union.rb', line 6

def types
  @types
end

Class Method Details

.new(*types) ⇒ Union, ...

Parameters:

  • types (Array<Base>)

Returns:



10
11
12
13
14
15
16
17
18
19
20
# File 'lib/yoda/typing/types/union.rb', line 10

def self.new(*types)
  extracted_types = types.map { |type| type.is_a?(Union) ? type.types : type }.flatten
  case extracted_types.length
  when 0
    Any.new
  when 1
    extracted_types.first
  else
    super(*extracted_types)
  end
end

Instance Method Details

#to_expressionObject



28
29
30
# File 'lib/yoda/typing/types/union.rb', line 28

def to_expression
  Model::TypeExpressions::UnionType.new(types.map(&:to_expression))
end

#to_type_stringObject



32
33
34
# File 'lib/yoda/typing/types/union.rb', line 32

def to_type_string
  types.map(&:to_type_string).join(" | ")
end