Runtime assertions for Ruby literal.fun
ruby
5
fork

Configure Feed

Select the types of activity you want to include in your feed.

Make unions more like other types

+37 -54
-3
lib/literal.rb
··· 16 16 autoload :Struct, "literal/struct" 17 17 autoload :Structish, "literal/structish" 18 18 autoload :Types, "literal/types" 19 - autoload :Union, "literal/union" 20 19 autoload :Visitor, "literal/visitor" 21 20 autoload :Null, "literal/null" 22 21 23 22 # Errors 24 23 autoload :TypeError, "literal/errors/type_error" 25 24 autoload :Error, "literal/errors/error" 26 - 27 - extend Literal::Types 28 25 end
+2 -1
lib/literal/types.rb
··· 30 30 autoload :SymbolType, "literal/types/symbol_type" 31 31 autoload :TruthyType, "literal/types/truthy_type" 32 32 autoload :TupleType, "literal/types/tuple_type" 33 + autoload :UnionType, "literal/types/union_type" 33 34 autoload :VoidType, "literal/types/void_type" 34 35 35 36 # Matches any value except `nil`. Use `_Nilable(_Any)` or `_Void` to match any value including `nil`. ··· 187 188 188 189 # Matches if *any* given type is matched. 189 190 def _Union(...) 190 - Literal::Union.new(...) 191 + Literal::Types::UnionType.new(...) 191 192 end 192 193 193 194 def _Void
+11
lib/literal/types.test.rb
··· 284 284 refute _Tuple(String, Integer) === nil 285 285 end 286 286 287 + test "_Union" do 288 + type = _Union(String, Integer) 289 + 290 + assert type === "string" 291 + assert type === 42 292 + 293 + refute type === :symbol 294 + refute type === [] 295 + refute type === nil 296 + end 297 + 287 298 test "_Void" do 288 299 Fixtures::Objects.each do |object| 289 300 assert _Void === object
+24
lib/literal/types/union_type.rb
··· 1 + # frozen_string_literal: true 2 + 3 + class Literal::Types::UnionType 4 + include Enumerable 5 + 6 + def initialize(*types) 7 + raise Literal::ArgumentError.new("_Union type must have at least one type.") if types.size < 1 8 + @types = types.to_set.freeze 9 + end 10 + 11 + def inspect = "_Union(#{@types.inspect})" 12 + 13 + def ===(value) 14 + @types.any? { |type| type === value } 15 + end 16 + 17 + def each(&) 18 + @types.each(&) 19 + end 20 + 21 + def deconstruct 22 + @types.to_a 23 + end 24 + end
-50
lib/literal/union.rb
··· 1 - # frozen_string_literal: true 2 - 3 - class Literal::Union 4 - include Enumerable 5 - 6 - def initialize(*types) 7 - raise Literal::ArgumentError.new("_Union type must have at least one type.") if types.size < 1 8 - @types = types 9 - end 10 - 11 - def inspect = "Literal::Union(#{@types.map(&:inspect).join(', ')})" 12 - 13 - def ===(value) 14 - @types.any? { |type| type === value } 15 - end 16 - 17 - def each(&) 18 - @types.each(&) 19 - end 20 - 21 - def [](key) 22 - index.fetch(key) 23 - end 24 - 25 - def index 26 - @index ||= @types.index_by(&:itself) 27 - end 28 - 29 - def handle(value, &) 30 - Literal::Variant.new(value, *@types).handle(&) 31 - end 32 - 33 - def variant(value = Literal::Null) 34 - if Literal::Null != value 35 - Literal::Variant.new(value, *@types) 36 - elsif block_given? 37 - Literal::Variant.new(yield, *@types) 38 - else 39 - Literal::VariantType.new(*@types) 40 - end 41 - end 42 - 43 - def deconstruct 44 - @types 45 - end 46 - 47 - def deconstruct_keys(_) 48 - { types: @types } 49 - end 50 - end