-
-
Notifications
You must be signed in to change notification settings - Fork 9.3k
/
typed_set.rb
51 lines (44 loc) 路 1.48 KB
/
typed_set.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
# frozen_string_literal: true
# typed: true
module T::Types
class TypedSet < TypedEnumerable
def underlying_class
Hash
end
# overrides Base
def name
"T::Set[#{type.name}]"
end
# overrides Base
def recursively_valid?(obj)
# Re-implements non_forcing_is_a?
return false if Object.autoload?(:Set) # Set is meant to be autoloaded but not yet loaded, this value can't be a Set
return false unless Object.const_defined?(:Set) # Set is not loaded yet
obj.is_a?(Set) && super
end
# overrides Base
def valid?(obj)
# Re-implements non_forcing_is_a?
return false if Object.autoload?(:Set) # Set is meant to be autoloaded but not yet loaded, this value can't be a Set
return false unless Object.const_defined?(:Set) # Set is not loaded yet
obj.is_a?(Set)
end
def new(*args)
# Fine for this to blow up, because hopefully if they're trying to make a
# Set, they don't mind putting (or already have put) a `require 'set'` in
# their program directly.
Set.new(*T.unsafe(args))
end
class Untyped < TypedSet
def initialize
super(T.untyped)
end
def valid?(obj)
# Re-implements non_forcing_is_a?
return false if Object.autoload?(:Set) # Set is meant to be autoloaded but not yet loaded, this value can't be a Set
return false unless Object.const_defined?(:Set) # Set is not loaded yet
obj.is_a?(Set)
end
end
end
end