Class: RDL::Type::OptionalType

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Type

#canonical, leq, #nil_type?, #to_contract, #vararg?

Constructor Details

#initialize(type) ⇒ OptionalType

Returns a new instance of OptionalType.

Raises:

  • (RuntimeError)


5
6
7
8
9
10
11
12
# File 'lib/rdl/types/optional.rb', line 5

def initialize(type)
  raise RuntimeError, "Attempt to create optional type with non-type" unless type.is_a? Type
  raise "Can't have optional optional type" if type.is_a? OptionalType
  raise "Can't have optional vararg type" if type.is_a? VarargType
  raise "Can't have optional annotated type" if type.is_a? AnnotatedArgType
  @type = type
  super()
end

Instance Attribute Details

#typeObject (readonly)

Returns the value of attribute type.



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

def type
  @type
end

Instance Method Details

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

:nodoc:



24
25
26
27
28
# File 'lib/rdl/types/optional.rb', line 24

def ==(other) # :nodoc:
  return false if other.nil?
  other = other.canonical
  return (other.instance_of? OptionalType) && (other.type == @type)
end

#copyObject



49
50
51
# File 'lib/rdl/types/optional.rb', line 49

def copy
  return OptionalType.new(@type.copy)
end

#hashObject

:nodoc:



53
54
55
# File 'lib/rdl/types/optional.rb', line 53

def hash # :nodoc:
  return 57 + @type.hash
end

#instantiate(inst) ⇒ Object

Note: no member?, because these can only appear in MethodType, where they’re handled specially



41
42
43
# File 'lib/rdl/types/optional.rb', line 41

def instantiate(inst)
  return OptionalType.new(@type.instantiate(inst))
end

#match(other) ⇒ Object



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

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

#optional?Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/rdl/types/optional.rb', line 57

def optional?
  return true
end

#to_sObject



14
15
16
17
18
19
20
21
22
# File 'lib/rdl/types/optional.rb', line 14

def to_s
  if @type.is_a? UnionType
    "?(#{@type.to_s})"
  elsif @type.is_a? MethodType
    "?{ #{@type.to_s} }"
  else
    "?#{@type.to_s}"
  end
end

#widenObject



45
46
47
# File 'lib/rdl/types/optional.rb', line 45

def widen
  return OptionalType.new(@type.widen)
end