Skip to main content

Symbol

Represents a unique object.

Symbols are often used as unique keys in tables. This is useful to avoid possible collisions with a key in a table, since the symbol will always be unique and cannot be reconstructed.

All Unique

Every creation of a symbol is unique, even if the given names are the same.

local Symbol = require(packages.Symbol)

-- Create a symbol:
local symbol = Symbol("MySymbol")

-- The name is optional:
local anotherSymbol = Symbol()

-- Comparison:
print(symbol == symbol) --> true

-- All symbol constructions are unique, regardless of the name:
print(Symbol("Hello") == Symbol("Hello")) --> false

-- Commonly used as unique keys in a table:
local DATA_KEY = Symbol("Data")
local t = {
	-- Can only be accessed using the DATA_KEY symbol:
	[DATA_KEY] = {}
}

print(t[DATA_KEY]) --> {}
Show raw api
{
    "functions": [],
    "properties": [],
    "types": [],
    "name": "Symbol",
    "desc": "Represents a unique object.\n\nSymbols are often used as unique keys in tables. This is useful to avoid possible collisions\nwith a key in a table, since the symbol will always be unique and cannot be reconstructed.\n\n\n:::note All Unique\nEvery creation of a symbol is unique, even if the\ngiven names are the same.\n:::\n\n```lua\nlocal Symbol = require(packages.Symbol)\n\n-- Create a symbol:\nlocal symbol = Symbol(\"MySymbol\")\n\n-- The name is optional:\nlocal anotherSymbol = Symbol()\n\n-- Comparison:\nprint(symbol == symbol) --> true\n\n-- All symbol constructions are unique, regardless of the name:\nprint(Symbol(\"Hello\") == Symbol(\"Hello\")) --> false\n\n-- Commonly used as unique keys in a table:\nlocal DATA_KEY = Symbol(\"Data\")\nlocal t = {\n\t-- Can only be accessed using the DATA_KEY symbol:\n\t[DATA_KEY] = {}\n}\n\nprint(t[DATA_KEY]) --> {}\n```",
    "source": {
        "line": 44,
        "path": "modules/symbol/init.lua"
    }
}