-
-
Notifications
You must be signed in to change notification settings - Fork 9.3k
/
simple_pair_union.rb
55 lines (48 loc) 路 1.39 KB
/
simple_pair_union.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# frozen_string_literal: true
# typed: true
# Specialization of Union for the common case of the union of two simple types.
#
# This covers e.g. T.nilable(SomeModule), T.any(Integer, Float), and T::Boolean.
class T::Private::Types::SimplePairUnion < T::Types::Union
class DuplicateType < RuntimeError; end
# @param type_a [T::Types::Simple]
# @param type_b [T::Types::Simple]
def initialize(type_a, type_b)
if type_a == type_b
raise DuplicateType.new("#{type_a} == #{type_b}")
end
@raw_a = type_a.raw_type
@raw_b = type_b.raw_type
end
# @override Union
def recursively_valid?(obj)
obj.is_a?(@raw_a) || obj.is_a?(@raw_b)
end
# @override Union
def valid?(obj)
obj.is_a?(@raw_a) || obj.is_a?(@raw_b)
end
# @override Union
def types
# We reconstruct the simple types rather than just storing them because
# (1) this is normally not a hot path and (2) we want to keep the instance
# variable count <= 3 so that we can fit in a 40 byte heap entry along
# with object headers.
@types ||= [
T::Types::Simple::Private::Pool.type_for_module(@raw_a),
T::Types::Simple::Private::Pool.type_for_module(@raw_b),
]
end
# overrides Union
def unwrap_nilable
a_nil = @raw_a.equal?(NilClass)
b_nil = @raw_b.equal?(NilClass)
if a_nil
return types[1]
end
if b_nil
return types[0]
end
nil
end
end