Class: Rigor::Type::Tuple
- Inherits:
-
Object
- Object
- Rigor::Type::Tuple
- 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
-
#elements ⇒ Object
readonly
Returns the value of attribute elements.
Instance Method Summary collapse
- #==(other) ⇒ Object (also: #eql?)
- #accepts(other, mode: :gradual) ⇒ Object
- #bot ⇒ Object
- #describe(verbosity = :short) ⇒ Object
- #dynamic ⇒ Object
- #erase_to_rbs ⇒ Object
- #hash ⇒ Object
-
#initialize(elements) ⇒ Tuple
constructor
A new instance of Tuple.
- #inspect ⇒ Object
- #top ⇒ Object
Constructor Details
#initialize(elements) ⇒ Tuple
Returns a new instance of Tuple.
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
#elements ⇒ Object (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 |
#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 |
#erase_to_rbs ⇒ Object
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 |
#hash ⇒ Object
75 76 77 |
# File 'lib/rigor/type/tuple.rb', line 75 def hash [Tuple, elements].hash end |
#inspect ⇒ Object
79 80 81 |
# File 'lib/rigor/type/tuple.rb', line 79 def inspect "#<Rigor::Type::Tuple #{describe(:short)}>" end |