Class: Literal::Types::UnionType

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/literal/types/union_type.rb

Instance Method Summary collapse

Constructor Details

#initialize(*types) ⇒ UnionType

Returns a new instance of UnionType.



6
7
8
9
10
11
12
# File 'lib/literal/types/union_type.rb', line 6

def initialize(*types)
	raise Literal::ArgumentError.new("_Union type must have at least one type.") if types.size < 1

	@types = Set[]
	load_types(types)
	@types.freeze
end

Instance Method Details

#===(value) ⇒ Object



16
17
18
# File 'lib/literal/types/union_type.rb', line 16

def ===(value)
	@types.any? { |type| type === value }
end

#deconstructObject



24
25
26
# File 'lib/literal/types/union_type.rb', line 24

def deconstruct
	@types.to_a
end

#eachObject



20
21
22
# File 'lib/literal/types/union_type.rb', line 20

def each(&)
	@types.each(&)
end

#inspectObject



14
# File 'lib/literal/types/union_type.rb', line 14

def inspect = "_Union(#{@types.inspect})"

#load_types(types) ⇒ Object (private)



34
35
36
37
38
# File 'lib/literal/types/union_type.rb', line 34

def load_types(types)
	types.each do |type|
		(Literal::Types::UnionType === type) ? load_types(type.types) : @types << type
	end
end