···33class Literal::Types::UnionType
44 include Enumerable
5566- def initialize(*types)
77- raise Literal::ArgumentError.new("_Union type must have at least one type.") if types.size < 1
66+ def initialize(*queue)
77+ raise Literal::ArgumentError.new("_Union type must have at least one type.") if queue.size < 1
88+ types = []
99+ primitives = Set[]
81099- @types = []
1010- load_types(types)
1111- @types.uniq!
1111+ while queue.length > 0
1212+ type = queue.shift
1313+ case type
1414+ when Literal::Types::UnionType
1515+ queue.concat(type.types, type.primitives.to_a)
1616+ when Array, Hash, String, Symbol, Integer, Float, Complex, Rational, true, false, nil
1717+ primitives << type
1818+ else
1919+ types << type
2020+ end
2121+ end
2222+2323+ types.uniq!
2424+ @types = types
2525+ @primitives = primitives
2626+1227 @types.freeze
2828+ @primitives.freeze
2929+ freeze
1330 end
14311515- attr_reader :types
3232+ attr_reader :types, :primitives
16331734 def inspect
1835 "_Union(#{@types.inspect})"
1936 end
20372138 def ===(value)
3939+ return true if @primitives.include?(value)
4040+2241 types = @types
4242+2343 i, len = 0, types.size
2444 while i < len
2545 return true if types[i] === value
···6282 @types.any? do |type|
6383 Literal.subtype?(other, of: type)
6484 end
6565- end
6666- end
6767-6868- private
6969-7070- def load_types(types)
7171- types.each do |type|
7272- (Literal::Types::UnionType === type) ? load_types(type.types) : @types << type
7385 end
7486 end
7587
+13
test/types.test.rb
···755755 assert_equal type.inspect, "_Union([String, Integer, Symbol, Float])"
756756end
757757758758+test "_Union with primitives" do
759759+ position = _Union(:top, :right, :bottom, :left, Integer)
760760+761761+ assert position === :top
762762+ assert position === :right
763763+ assert position === :bottom
764764+ assert position === :left
765765+ assert position === 42
766766+767767+ refute position === :center
768768+ refute position === "top"
769769+end
770770+758771test "_Void" do
759772 Fixtures::Objects.each do |object|
760773 assert _Void === object