Class: Rigor::Type::Nominal

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

Overview

An instance type for a Ruby class or module. The class is identified by its fully-qualified Ruby name; the registry attached to the environment owns the class lookup.

Slice 4 phase 2d adds ‘type_args`: an ordered, frozen array of `Rigor::Type` values that carry the receiver’s generic instantiation. The empty array is the canonical “raw” form (‘Nominal`); a non-empty array represents an applied generic (`Nominal[Array, [Integer]]`). Two Nominals are structurally equal only when their `class_name` AND `type_args` match, so the raw form and any applied form are intentionally distinct values. Acceptance routes treat the raw form leniently for backward compatibility with phase 2b call sites that have not yet learned to carry generics.

Type arguments MUST be ‘Rigor::Type` instances. The constructor freezes the array; callers MUST NOT mutate it after construction.

See docs/type-specification/rbs-compatible-types.md.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(class_name, type_args = []) ⇒ Nominal

Returns a new instance of Nominal.

Raises:

  • (ArgumentError)


29
30
31
32
33
34
35
36
37
# File 'lib/rigor/type/nominal.rb', line 29

def initialize(class_name, type_args = [])
  raise ArgumentError, "class_name must be a String, got #{class_name.class}" unless class_name.is_a?(String)
  raise ArgumentError, "class_name must not be empty" if class_name.empty?
  raise ArgumentError, "type_args must be an Array, got #{type_args.class}" unless type_args.is_a?(Array)

  @class_name = class_name.freeze
  @type_args = type_args.dup.freeze
  freeze
end

Instance Attribute Details

#class_nameObject (readonly)

Returns the value of attribute class_name.



27
28
29
# File 'lib/rigor/type/nominal.rb', line 27

def class_name
  @class_name
end

#type_argsObject (readonly)

Returns the value of attribute type_args.



27
28
29
# File 'lib/rigor/type/nominal.rb', line 27

def type_args
  @type_args
end

Instance Method Details

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



69
70
71
# File 'lib/rigor/type/nominal.rb', line 69

def ==(other)
  other.is_a?(Nominal) && class_name == other.class_name && type_args == other.type_args
end

#accepts(other, mode: :gradual) ⇒ Object



65
66
67
# File 'lib/rigor/type/nominal.rb', line 65

def accepts(other, mode: :gradual)
  Inference::Acceptance.accepts(self, other, mode: mode)
end

#botObject



57
58
59
# File 'lib/rigor/type/nominal.rb', line 57

def bot
  Trinary.no
end

#describe(verbosity = :short) ⇒ Object



39
40
41
42
43
44
# File 'lib/rigor/type/nominal.rb', line 39

def describe(verbosity = :short)
  return class_name if type_args.empty?

  rendered = type_args.map { |t| t.describe(verbosity) }.join(", ")
  "#{class_name}[#{rendered}]"
end

#dynamicObject



61
62
63
# File 'lib/rigor/type/nominal.rb', line 61

def dynamic
  Trinary.no
end

#erase_to_rbsObject



46
47
48
49
50
51
# File 'lib/rigor/type/nominal.rb', line 46

def erase_to_rbs
  return class_name if type_args.empty?

  rendered = type_args.map(&:erase_to_rbs).join(", ")
  "#{class_name}[#{rendered}]"
end

#hashObject



74
75
76
# File 'lib/rigor/type/nominal.rb', line 74

def hash
  [Nominal, class_name, type_args].hash
end

#inspectObject



78
79
80
# File 'lib/rigor/type/nominal.rb', line 78

def inspect
  "#<Rigor::Type::Nominal #{describe(:short)}>"
end

#topObject



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

def top
  Trinary.no
end