Class: RDL::Type::IntersectionType
- Defined in:
- lib/rdl/types/intersection.rb
Constant Summary collapse
- @@cache =
{}
Instance Attribute Summary collapse
-
#types ⇒ Object
readonly
Returns the value of attribute types.
Class Method Summary collapse
Instance Method Summary collapse
-
#==(other) ⇒ Object
(also: #eql?)
:nodoc:.
-
#hash ⇒ Object
:nodoc:.
-
#initialize(types) ⇒ IntersectionType
constructor
A new instance of IntersectionType.
- #instantiate(inst) ⇒ Object
- #match(other) ⇒ Object
- #member?(obj, *args) ⇒ Boolean
-
#to_s ⇒ Object
:nodoc:.
Methods inherited from Type
#canonical, leq, #nil_type?, #optional?, #to_contract, #vararg?
Constructor Details
#initialize(types) ⇒ IntersectionType
Returns a new instance of IntersectionType.
37 38 39 40 |
# File 'lib/rdl/types/intersection.rb', line 37 def initialize(types) @types = types super() end |
Instance Attribute Details
#types ⇒ Object (readonly)
Returns the value of attribute types.
5 6 7 |
# File 'lib/rdl/types/intersection.rb', line 5 def types @types end |
Class Method Details
.__new__ ⇒ Object
10 |
# File 'lib/rdl/types/intersection.rb', line 10 alias :__new__ :new |
.new(*types) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/rdl/types/intersection.rb', line 13 def self.new(*types) ts = [] types.each { |t| if t == RDL::Globals.types[:bot] next elsif t.instance_of? IntersectionType ts.concat t.types else raise RuntimeError, "Attempt to create intersection type with non-type" unless t.is_a? Type ts << t end } ts.sort! { |a,b| a.object_id <=> b.object_id } ts.uniq! return RDL::Globals.types[:bot] if ts.size == 0 return ts[0] if ts.size == 1 t = @@cache[ts] return t if t t = IntersectionType.__new__(ts) return (@@cache[ts] = t) # assignment evaluates to t end |
Instance Method Details
#==(other) ⇒ Object Also known as: eql?
:nodoc:
46 47 48 49 50 51 |
# File 'lib/rdl/types/intersection.rb', line 46 def ==(other) # :nodoc: return false if other.nil? other = other.type if other.is_a? DependentArgType other = other.canonical return (other.instance_of? IntersectionType) && (other.types == @types) end |
#hash ⇒ Object
:nodoc:
72 73 74 |
# File 'lib/rdl/types/intersection.rb', line 72 def hash # :nodoc: return 47 + @types.hash end |
#instantiate(inst) ⇒ Object
68 69 70 |
# File 'lib/rdl/types/intersection.rb', line 68 def instantiate(inst) return IntersectionType.new(*(@types.map { |t| t.instantiate(inst) })) end |
#match(other) ⇒ Object
55 56 57 58 59 60 61 62 |
# File 'lib/rdl/types/intersection.rb', line 55 def match(other) other = other.canonical other = other.type if other.instance_of? AnnotatedArgType return true if other.instance_of? WildQuery return false unless other.instance_of? IntersectionType return false if @types.length != other.types.length @types.all? { |t| other.types.any? { |ot| t.match(ot) } } end |
#member?(obj, *args) ⇒ Boolean
64 65 66 |
# File 'lib/rdl/types/intersection.rb', line 64 def member?(obj, *args) @types.all? { |t| t.member?(obj, *args) } end |
#to_s ⇒ Object
:nodoc:
42 43 44 |
# File 'lib/rdl/types/intersection.rb', line 42 def to_s # :nodoc: return "(#{@types.map { |t| t.to_s }.join(' and ')})" end |