···3030 autoload :SymbolType, "literal/types/symbol_type"
3131 autoload :TruthyType, "literal/types/truthy_type"
3232 autoload :TupleType, "literal/types/tuple_type"
3333+ autoload :UnionType, "literal/types/union_type"
3334 autoload :VoidType, "literal/types/void_type"
34353536 # Matches any value except `nil`. Use `_Nilable(_Any)` or `_Void` to match any value including `nil`.
···187188188189 # Matches if *any* given type is matched.
189190 def _Union(...)
190190- Literal::Union.new(...)
191191+ Literal::Types::UnionType.new(...)
191192 end
192193193194 def _Void
+11
lib/literal/types.test.rb
···284284 refute _Tuple(String, Integer) === nil
285285end
286286287287+test "_Union" do
288288+ type = _Union(String, Integer)
289289+290290+ assert type === "string"
291291+ assert type === 42
292292+293293+ refute type === :symbol
294294+ refute type === []
295295+ refute type === nil
296296+end
297297+287298test "_Void" do
288299 Fixtures::Objects.each do |object|
289300 assert _Void === object
+24
lib/literal/types/union_type.rb
···11+# frozen_string_literal: true
22+33+class Literal::Types::UnionType
44+ include Enumerable
55+66+ def initialize(*types)
77+ raise Literal::ArgumentError.new("_Union type must have at least one type.") if types.size < 1
88+ @types = types.to_set.freeze
99+ end
1010+1111+ def inspect = "_Union(#{@types.inspect})"
1212+1313+ def ===(value)
1414+ @types.any? { |type| type === value }
1515+ end
1616+1717+ def each(&)
1818+ @types.each(&)
1919+ end
2020+2121+ def deconstruct
2222+ @types.to_a
2323+ end
2424+end
-50
lib/literal/union.rb
···11-# frozen_string_literal: true
22-33-class Literal::Union
44- include Enumerable
55-66- def initialize(*types)
77- raise Literal::ArgumentError.new("_Union type must have at least one type.") if types.size < 1
88- @types = types
99- end
1010-1111- def inspect = "Literal::Union(#{@types.map(&:inspect).join(', ')})"
1212-1313- def ===(value)
1414- @types.any? { |type| type === value }
1515- end
1616-1717- def each(&)
1818- @types.each(&)
1919- end
2020-2121- def [](key)
2222- index.fetch(key)
2323- end
2424-2525- def index
2626- @index ||= @types.index_by(&:itself)
2727- end
2828-2929- def handle(value, &)
3030- Literal::Variant.new(value, *@types).handle(&)
3131- end
3232-3333- def variant(value = Literal::Null)
3434- if Literal::Null != value
3535- Literal::Variant.new(value, *@types)
3636- elsif block_given?
3737- Literal::Variant.new(yield, *@types)
3838- else
3939- Literal::VariantType.new(*@types)
4040- end
4141- end
4242-4343- def deconstruct
4444- @types
4545- end
4646-4747- def deconstruct_keys(_)
4848- { types: @types }
4949- end
5050-end