Class: Rigor::Type::Tuple

Inherits:
Object
  • Object
show all
Includes:
AcceptanceRouter, ValueSemantics
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

Methods included from ValueSemantics

included

Methods included from AcceptanceRouter

#accepts

Constructor Details

#initialize(elements) ⇒ Tuple

Returns a new instance of Tuple.

Raises:

  • (ArgumentError)


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

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.



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

def elements
  @elements
end

Instance Method Details

#botObject



60
61
62
# File 'lib/rigor/type/tuple.rb', line 60

def bot
  Trinary.no
end

#describe(verbosity = :short) ⇒ Object



44
45
46
47
48
# File 'lib/rigor/type/tuple.rb', line 44

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

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

#dynamicObject



64
65
66
# File 'lib/rigor/type/tuple.rb', line 64

def dynamic
  Trinary.no
end

#erase_to_rbsObject



50
51
52
53
54
# File 'lib/rigor/type/tuple.rb', line 50

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

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

#inspectObject



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

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

#topObject



56
57
58
# File 'lib/rigor/type/tuple.rb', line 56

def top
  Trinary.no
end