Class: RDL::Type::SingletonType

Inherits:
Type show all
Defined in:
lib/rdl/types/singleton.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(val) ⇒ SingletonType



20
21
22
23
# File 'lib/rdl/types/singleton.rb', line 20

def initialize(val)
  @val = val
  @nominal = NominalType.new(val.class)
end

Instance Attribute Details

#nominalObject (readonly)

Returns the value of attribute nominal.



4
5
6
# File 'lib/rdl/types/singleton.rb', line 4

def nominal
  @nominal
end

#valObject

Returns the value of attribute val.



3
4
5
# File 'lib/rdl/types/singleton.rb', line 3

def val
  @val
end

Class Method Details

.__new__Object



10
# File 'lib/rdl/types/singleton.rb', line 10

alias :__new__ :new

.new(val) ⇒ Object



13
14
15
16
17
18
# File 'lib/rdl/types/singleton.rb', line 13

def self.new(val)
  t = @@cache[val]
  return t if t
  t = self.__new__ val
  return (@@cache[val] = t) # assignment evaluates to t
end

Instance Method Details

#<=(other) ⇒ Object



55
56
57
# File 'lib/rdl/types/singleton.rb', line 55

def <=(other)
  return Type.leq(self, other)
end

#==(other) ⇒ Object Also known as: eql?



25
26
27
28
29
# File 'lib/rdl/types/singleton.rb', line 25

def ==(other)
  return false if other.nil?
  other = other.canonical
  return (other.instance_of? self.class) && (other.val.equal? @val)
end

#copyObject



74
75
76
# File 'lib/rdl/types/singleton.rb', line 74

def copy
  return self
end

#hashObject

:nodoc:



40
41
42
# File 'lib/rdl/types/singleton.rb', line 40

def hash # :nodoc:
  return @val.hash
end

#instantiate(inst) ⇒ Object



66
67
68
# File 'lib/rdl/types/singleton.rb', line 66

def instantiate(inst)
  return self
end

#match(other) ⇒ Object



33
34
35
36
37
38
# File 'lib/rdl/types/singleton.rb', line 33

def match(other)
  other = other.canonical
  other = other.type if other.instance_of? AnnotatedArgType
  return true if other.instance_of? WildQuery
  return self == other
end

#member?(obj, *args) ⇒ Boolean



59
60
61
62
63
64
# File 'lib/rdl/types/singleton.rb', line 59

def member?(obj, *args)
  t = RDL::Util.rdl_type obj
  return t <= self if t
  return true if obj.nil?
  obj.equal?(@val)
end

#satisfies? {|val| ... } ⇒ Boolean

Yields:



78
79
80
# File 'lib/rdl/types/singleton.rb', line 78

def satisfies?
  yield(val)
end

#to_sObject



44
45
46
47
48
49
50
51
52
53
# File 'lib/rdl/types/singleton.rb', line 44

def to_s
  if @val.instance_of? Symbol
    ":#{@val}"
  elsif @val.nil?
    "nil"
  else
    @val.to_s
#        "Singleton(#{@val.to_s})"
  end
end

#widenObject



70
71
72
# File 'lib/rdl/types/singleton.rb', line 70

def widen
  return self
end