A symbol in computer programming is a primitive data type whose instances have a human-readable form. Symbols can be used as identifiers. In some programming languages, they are called atoms.[1] Uniqueness is enforced by holding them in a symbol table. The most common use of symbols by programmers is to perform language reflection (particularly for callbacks), and the most common indirectly is their use to create object linkages.
In the most trivial implementation, they are essentially named integers; e.g., the enumerated type in C language.
The following programming languages provide runtime support for symbols:
language | type name(s) | example literal(s) | |
---|---|---|---|
symbol, keyword | symbol , :keyword | ||
symbol,[2] keyword[3] | 'symbol , :keyword | ||
Symbol[4] | #sym | ||
atom, symbol | :sym | ||
atom | sym or 'sym' | ||
JavaScript (ES6 and later) | Symbol | Symbol("sym"); | |
Symbol | :sym | ||
symbol | `sym | ||
SEL | @selector(sym) | ||
symbol | symbol let name = variable | ||
name | /sym or sym | ||
atom, symbol | sym or 'sym' | ||
Symbol | :sym or :'sym' | ||
scala.Symbol | 'symbol | ||
symbol | sym | ||
Symbol | #sym or #'sym' | ||
Atom.atom | |||
Symbol | Symbol["sym"] or sym |
Symbols in Julia are interned strings used to represent identifiers in parsed Julia code(ASTs) and as names or labels to identify entities (for example as keys in a dictionary).[5]
A symbol in Lisp is unique in a namespace (or package in Common Lisp). Symbols can be tested for equality with the function EQ. Lisp programs can generate new symbols at runtime. When Lisp reads data that contains textual represented symbols, existing symbols are referenced. If a symbol is unknown, the Lisp reader creates a new symbol.
In Common Lisp, symbols have the following attributes: a name, a value, a function, a list of properties and a package.[6]
In Common Lisp it is also possible that a symbol is not interned in a package. Such symbols can be printed, but when read back, a new symbol needs to be created. Since it is not interned, the original symbol can not be retrieved from a package.
In Common Lisp symbols may use any characters, including whitespace, such as spaces and newlines. If a symbol contains a whitespace character, it needs to be written as |this is a symbol|. Symbols can be used as identifiers for any kind of named programming constructs: variables, functions, macros, classes, types, goto tags and more.Symbols can be interned in a package.[7] Keyword symbols are self-evaluating,[8] and interned in the package named KEYWORD.
The following is a simple external representation of a Common Lisp symbol:
Symbols can contain whitespace (and all other characters):
In Common Lisp symbols with a leading colon in their printed representations are keyword symbols. These are interned in the keyword package.
keyword-symbol
A printed representation of a symbol may include a package name. Two colons are written between the name of the package and the name of the symbol.
Packages can export symbols. Then only one colon is written between the name of the package and the name of the symbol.
Symbols, which are not interned in a package, can also be created and have a notation:
uninterned-symbol
In PostScript, references to name objects can be either literal or executable, influencing the behaviour of the interpreter when encountering them. The cvx
and cvl
operators can be used to convert between the two forms. When names are constructed from strings by means of the cvn
operator, the set of allowed characters is unrestricted.
In Prolog, symbols (or atoms) are the main primitive data types, similar to numbers.[9] The exact notation may differ in different Prolog dialects. However, it is always quite simple (no quotations or special beginning characters are necessary).
Contrary to many other languages, it is possible to give symbols a meaning by creating some Prolog facts and/or rules.
The following example demonstrates two facts (describing what father is) and one rule (describing the meaning of sibling). These three sentences use symbols (father, zeus, hermes, perseus and sibling) and some abstract variables (X, Y and Z). The mother relationship is omitted for clarity.
sibling(X, Y) :- father(Z, X), father(Z, Y).
In Ruby, symbols can be created with a literal form, or by converting a string.They can be used as an identifier or an interned string.[10] Two symbols with the same contents will always refer to the same object.[11] It is considered a best practice to use symbols as keys to an associative array in Ruby.[12]
The following is a simple example of a symbol literal in Ruby:Symbol
class in Ruby:[13]
In Smalltalk, symbols can be created with a literal form, or by converting a string. They can be used as an identifier or an interned string. Two symbols with the same contents will always refer to the same object.[14] In most Smalltalk implementations, selectors (method names) are implemented as symbols.
The following is a simple example of a symbol literal in Smalltalk:symbol
protocol, and their class is called Symbol
in most implementations: