Class: Rigor::Type::Tuple

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

Overview

A heterogeneous, fixed-arity array shape. Inhabitants are exactly the Ruby ‘Array` instances whose length matches `elements.size` and whose element at position `i` inhabits `elements`.

In RBS this corresponds to the tuple form ‘[A, B, C]`. A tuple is always a subtype of `Array`; method dispatch therefore degrades to the underlying `Nominal[Array, [union]]` while acceptance keeps the per-position precision.

Slice 5 phase 1 introduces the carrier and surfaces it from the ‘ArrayNode` literal handler when every element is a non-splat value. Tuple-aware refinements for `tuple`, `tuple.first`, and destructuring assignment are deferred to Slice 5 phase 2; they will run as a higher-priority dispatch tier above Inference::MethodDispatcher::RbsDispatch.

Equality and hashing are structural across an ordered, frozen element list. The empty Tuple ‘Tuple[]` is permitted; the array literal handler keeps `[]` as raw `Nominal` (no element evidence to lock the arity), but external constructors MAY build `Tuple[]` directly when the zero-arity discipline is intended.

See docs/type-specification/rbs-compatible-types.md (tuple) and docs/type-specification/rigor-extensions.md (hash-shape and tuple kin).

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(elements) ⇒ Tuple

Returns a new instance of Tuple.

Raises:

  • (ArgumentError)


35
36
37
38
39
40
# File 'lib/rigor/type/tuple.rb', line 35

def initialize(elements)
  raise ArgumentError, "elements must be an Array, got #{elements.class}" unless elements.is_a?(Array)

  @elements = elements.dup.freeze
  freeze
end

Instance Attribute Details

#elementsObject (readonly)

Returns the value of attribute elements.



33
34
35
# File 'lib/rigor/type/tuple.rb', line 33

def elements
  @elements
end

Instance Method Details

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



70
71
72
# File 'lib/rigor/type/tuple.rb', line 70

def ==(other)
  other.is_a?(Tuple) && elements == other.elements
end

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



66
67
68
# File 'lib/rigor/type/tuple.rb', line 66

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

#botObject



58
59
60
# File 'lib/rigor/type/tuple.rb', line 58

def bot
  Trinary.no
end

#describe(verbosity = :short) ⇒ Object



42
43
44
45
46
# File 'lib/rigor/type/tuple.rb', line 42

def describe(verbosity = :short)
  return "[]" if elements.empty?

  "[#{elements.map { |t| t.describe(verbosity) }.join(', ')}]"
end

#dynamicObject



62
63
64
# File 'lib/rigor/type/tuple.rb', line 62

def dynamic
  Trinary.no
end

#erase_to_rbsObject



48
49
50
51
52
# File 'lib/rigor/type/tuple.rb', line 48

def erase_to_rbs
  return "[]" if elements.empty?

  "[#{elements.map(&:erase_to_rbs).join(', ')}]"
end

#hashObject



75
76
77
# File 'lib/rigor/type/tuple.rb', line 75

def hash
  [Tuple, elements].hash
end

#inspectObject



79
80
81
# File 'lib/rigor/type/tuple.rb', line 79

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

#topObject



54
55
56
# File 'lib/rigor/type/tuple.rb', line 54

def top
  Trinary.no
end