Class: Yadriggy::Type Abstract

Inherits:
Object
  • Object
show all
Defined in:
lib/yadriggy/type.rb

Overview

This class is abstract.

Root of the classes representing a type. A Type object may consist of a chain of multiple Type objects.

Don’t use ‘is_a?` but use `has_role?` or `role`.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.error_found!(msg) ⇒ Object

Raises an error.

Parameters:

  • msg (String)

    an error message.

Raises:



18
19
20
# File 'lib/yadriggy/type.rb', line 18

def self.error_found!(msg)
  raise TypeChecker::CheckError.new(msg)
end

.get_instance_method_object(recv_type, method_name) ⇒ Object



12
13
14
# File 'lib/yadriggy/type.rb', line 12

def self.get_instance_method_object(recv_type, method_name)
  recv_type.get_method_object(method_name)
end

.role(type) ⇒ Type|nil

Finds an instance of the receiver class in the chain starting with the given Yadriggy::Type object. If such an instance is not found, the method returns nil. Also see OptionalRole.

Parameters:

  • type (Type)

    a Type object.

Returns:

  • (Type|nil)

    an instance of the receiver class.



70
71
72
73
74
75
76
# File 'lib/yadriggy/type.rb', line 70

def self.role(type)
  if type.is_a?(Type)
    type.has_role?(self)
  else
    nil
  end
end

Instance Method Details

#!=(t) ⇒ Boolean

Check the inequality.

Returns:

  • (Boolean)

    false if ‘self` and `t` represent the same type.



40
41
42
# File 'lib/yadriggy/type.rb', line 40

def != (t)
  !(self == t)
end

#<=(t) ⇒ Boolean

Check the subtype relation.

Parameters:

  • t (Type)

    the other type.

Returns:

  • (Boolean)

    true if ‘self` is equivalent to `t` or a subtpye of `t`



53
54
55
# File 'lib/yadriggy/type.rb', line 53

def <= (t)
  self == t
end

#copy(without_role) ⇒ Type

Makes a copy of self. Note that a Yadriggy::Type object may consist of a chain of multiple Yadriggy::Type objects. The copied chain does not contain an instance of ‘without_role`.

Parameters:

Returns:

  • (Type)

    the copy.



34
35
36
# File 'lib/yadriggy/type.rb', line 34

def copy(without_role)
  self
end

#eql?(t) ⇒ Boolean

An alias to ‘==`.

Returns:

  • (Boolean)

    true if ‘self` and `t` represent the same type.



46
47
48
# File 'lib/yadriggy/type.rb', line 46

def eql?(t)
  self == t
end

#exact_typeModule|DynType

Gets the Ruby class represented by this Type.

Returns:



95
96
97
# File 'lib/yadriggy/type.rb', line 95

def exact_type
  DynType
end

#get_method_object(method_name) ⇒ Method|nil

Gets a method with the given name declared in this type. ‘nil` is returned when the method is not exactly determined.

Returns:

  • (Method|nil)


104
105
106
# File 'lib/yadriggy/type.rb', line 104

def get_method_object(method_name)
  nil
end

#has_role?(a_role) ⇒ Type|nil

Finds an instance of the receiver class in the chain starting with ‘self`. If such an instance is not found, the method returns `nil`. Also see OptionalRole.

Parameters:

  • a_role (Module)

    a subclass of Type.

Returns:

  • (Type|nil)

    an instance of ‘a_role`.



85
86
87
88
89
90
91
# File 'lib/yadriggy/type.rb', line 85

def has_role?(a_role)
  if self.is_a?(a_role)
    self
  else
    nil
  end
end

#is_super_of?(t) ⇒ Boolean

Only DynType, UnionType and OptionalRole override this method.

Returns:

  • (Boolean)


59
60
61
# File 'lib/yadriggy/type.rb', line 59

def is_super_of? (t)
  false
end

#nameString

Obtains the name of this type.

Returns:

  • (String)

    the type name.



24
25
26
# File 'lib/yadriggy/type.rb', line 24

def name()
  to_s
end

#supertypeType

Returns the type containing a wider range of values.

Returns:

  • (Type)

    the type containing a wider range of values.



109
110
111
# File 'lib/yadriggy/type.rb', line 109

def supertype
  nil
end