Module std.functional.tuple

Tuple container.

An interned, immutable, nil-preserving tuple object.

Like Lua strings, tuples with the same elements can be quickly compared with a straight-forward == comparison.

The immutability guarantees only work if you don't change the contents of tables after adding them to a tuple. Don't do that!

Objects

Tuple Immutable Tuple container.

Metamethods

__call ([i=1[, j=self.n]]) Unpack tuple values between index i and j, inclusive.
prototype:__len () Return the length of this tuple.
prototype:__newindex (k, v) Prevent mutation of tup.
prototype:__tostring () Return a string representation of tup


Objects

Tuple
Immutable Tuple container.

Fields:

  • _type string object name (default 'Tuple')
  • n integer number of tuple elements

Usage:

    local Tuple = require 'std.functional.tuple'
    function count(...)
       argtuple = Tuple(...)
       return argtuple.n
    end
    count() --> 0
    count(nil) --> 1
    count(false) --> 1
    count(false, nil, true, nil) --> 4

Metamethods

__call ([i=1[, j=self.n]])
Unpack tuple values between index i and j, inclusive.

Parameters:

  • i integer first index to unpack (default 1)
  • j integer last index to unpack (default self.n)

Returns:

    ... values at indices i through j, inclusive

Usage:

    tup = Tuple(1, 3, 2, 5)
    --> 3, 2, 5
    tup(2)
prototype:__len ()
Return the length of this tuple.

Returns:

    int number of elements in tup

Usage:

    -- Only works on Lua 5.2 or newer:
    #Tuple(nil, 2, nil) --> 3
    -- For compatibility with Lua 5.1, use operator.len
    len(Tuple(nil, 2, nil)
prototype:__newindex (k, v)
Prevent mutation of tup.

Parameters:

  • k tuple key
  • v tuple value

Raises:

cannot change immutable tuple object
prototype:__tostring ()
Return a string representation of tup

Returns:

    string representation of tup

Usage:

    -- "Tuple('nil', nil, false)"
    print(Tuple('nil', nil, false))
generated by LDoc 1.5.0 Last updated 2026-10-28 08:32:20