Class: Rigor::Type::Tuple

Inherits:
Object
  • Object
show all
Includes:
AcceptanceRouter, PlainLattice, ValueSemantics
Defined in:
lib/rigor/type/tuple.rb,
sig/rigor/type.rbs

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[i].

In RBS this corresponds to the tuple form [A, B, C]. A tuple is always a subtype of Array[union(elements)]; 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 (tuple[0], tuple.first, destructuring) are implemented in ShapeDispatch, which runs 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[Array] (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

Constructor Details

#initialize(elements) ⇒ Tuple

Returns a new instance of Tuple.

Parameters:

  • elements (Array[Type::t])

Raises:

  • (ArgumentError)


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

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

#elementsArray[Type::t] (readonly)

Returns the value of attribute elements.

Returns:

  • (Array[Type::t])


30
31
32
# File 'lib/rigor/type/tuple.rb', line 30

def elements
  @elements
end

Instance Method Details

#==Boolean

Parameters:

  • other (Object)

Returns:

  • (Boolean)


220
# File 'sig/rigor/type.rbs', line 220

def ==: (untyped other) -> bool

#acceptsAcceptsResult

Parameters:

  • other (Type::t)
  • mode: (accepts_mode)

Returns:



219
# File 'sig/rigor/type.rbs', line 219

def accepts: (Type::t other, ?mode: accepts_mode) -> AcceptsResult

#botTrinary

Returns:



217
# File 'sig/rigor/type.rbs', line 217

def bot: () -> Trinary

#describe(verbosity = :short) ⇒ String

Parameters:

  • verbosity (Symbol) (defaults to: :short)

Returns:

  • (String)


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

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

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

#dynamicTrinary

Returns:



218
# File 'sig/rigor/type.rbs', line 218

def dynamic: () -> Trinary

#erase_to_rbsString

Returns:

  • (String)


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

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

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

#hashInteger

Returns:

  • (Integer)


221
# File 'sig/rigor/type.rbs', line 221

def hash: () -> Integer

#inspectString

Returns:

  • (String)


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

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

#topTrinary

Returns:



216
# File 'sig/rigor/type.rbs', line 216

def top: () -> Trinary