Class: RDL::Type::IntersectionType

Inherits:
Type show all
Defined in:
lib/rdl/types/intersection.rb

Constant Summary collapse

@@cache =
{}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#typesObject (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.nil_type?
      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_nil_type 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

#hashObject

:nodoc:



71
72
73
# File 'lib/rdl/types/intersection.rb', line 71

def hash  # :nodoc:
  return 47 + @types.hash
end

#instantiate(inst) ⇒ Object



67
68
69
# File 'lib/rdl/types/intersection.rb', line 67

def instantiate(inst)
  return IntersectionType.new(*(@types.map { |t| t.instantiate(inst) }))
end

#match(other) ⇒ Object



55
56
57
58
59
60
61
# 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 if @types.length != other.types.length
  @types.all? { |t| other.types.any? { |ot| t.match(ot) } }
end

#member?(obj, *args) ⇒ Boolean

Returns:

  • (Boolean)


63
64
65
# File 'lib/rdl/types/intersection.rb', line 63

def member?(obj, *args)
  @types.all? { |t| t.member?(obj, *args) }
end

#to_sObject

: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