A symbol in
computer programming
Computer programming or coding is the composition of sequences of instructions, called computer program, programs, that computers can follow to perform tasks. It involves designing and implementing algorithms, step-by-step specifications of proc ...
is a
primitive data type
In computer science and computer programming, a data type (or simply type) is a collection or grouping of data values, usually specified by a set of possible values, a set of allowed operations on these values, and/or a representation of these ...
whose
instances have a human-readable form. Symbols can be used as
identifier
An identifier is a name that identifies (that is, labels the identity of) either a unique object or a unique ''class'' of objects, where the "object" or class may be an idea, person, physical countable object (or class thereof), or physical mass ...
s. In some
programming language
A programming language is a system of notation for writing computer programs.
Programming languages are described in terms of their Syntax (programming languages), syntax (form) and semantics (computer science), semantics (meaning), usually def ...
s, they are called atoms.
Uniqueness is enforced by holding them in a
symbol table
In computer science, a symbol table is a data structure used by a language translator such as a compiler or interpreter, where each identifier, symbol, constant, procedure and function in a program's source code is associated with information ...
. The most common use of symbols by programmers is to perform language
reflection (particularly for
callbacks
In computer programming, a callback is a function that is stored as data (a reference) and designed to be called by another function often ''back'' to the original abstraction layer.
A function that accepts a callback parameter may be design ...
), and the most common indirectly is their use to create object
linkages.
In the most trivial
implementation
Implementation is the realization of an application, execution of a plan, idea, scientific modelling, model, design, specification, Standardization, standard, algorithm, policy, or the Management, administration or management of a process or Goal ...
, they are essentially named
integer
An integer is the number zero (0), a positive natural number (1, 2, 3, ...), or the negation of a positive natural number (−1, −2, −3, ...). The negations or additive inverses of the positive natural numbers are referred to as negative in ...
s; e.g., the
enumerated type
In computer programming, an enumerated type (also called enumeration, enum, or factor in the R (programming language), R programming language, a status variable in the JOVIAL programming language, and a categorical variable in statistics) is a data ...
in
C language.
Support
The following
programming language
A programming language is a system of notation for writing computer programs.
Programming languages are described in terms of their Syntax (programming languages), syntax (form) and semantics (computer science), semantics (meaning), usually def ...
s provide
runtime support for symbols:
Julia
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).
Lisp
A symbol in
Lisp
Lisp (historically LISP, an abbreviation of "list processing") is a family of programming languages with a long history and a distinctive, fully parenthesized Polish notation#Explanation, prefix notation.
Originally specified in the late 1950s, ...
is unique in a
namespace
In computing, a namespace is a set of signs (''names'') that are used to identify and refer to objects of various kinds. A namespace ensures that all of a given set of objects have unique names so that they can be easily identified.
Namespaces ...
(or ''package'' in
Common Lisp
Common Lisp (CL) is a dialect of the Lisp programming language, published in American National Standards Institute (ANSI) standard document ''ANSI INCITS 226-1994 (S2018)'' (formerly ''X3.226-1994 (R1999)''). The Common Lisp HyperSpec, a hyperli ...
). 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.
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. Keyword symbols are self-evaluating, and interned in the package named KEYWORD.
Examples
The following is a simple external representation of a
Common Lisp
Common Lisp (CL) is a dialect of the Lisp programming language, published in American National Standards Institute (ANSI) standard document ''ANSI INCITS 226-1994 (S2018)'' (formerly ''X3.226-1994 (R1999)''). The Common Lisp HyperSpec, a hyperli ...
symbol:
this-is-a-symbol
Symbols can contain whitespace (and all other characters):
, This is a symbol with whitespace,
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.
package-name::symbol-name
Packages can export symbols. Then only one colon is written between the name of the package and the name of the symbol.
package:exported-symbol
Symbols, which are not interned in a package, can also be created and have a notation:
#:uninterned-symbol
PostScript
In
PostScript
PostScript (PS) is a page description language and dynamically typed, stack-based programming language. It is most commonly used in the electronic publishing and desktop publishing realm, but as a Turing complete programming language, it c ...
, 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.
Prolog
In
Prolog
Prolog is a logic programming language that has its origins in artificial intelligence, automated theorem proving, and computational linguistics.
Prolog has its roots in first-order logic, a formal logic. Unlike many other programming language ...
, symbols (or atoms) are the main primitive data types, similar to numbers.
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.
Examples
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.
father(zeus, hermes).
father(zeus, perseus).
sibling(X, Y) :- father(Z, X), father(Z, Y).
Ruby
In
Ruby
Ruby is a pinkish-red-to-blood-red-colored gemstone, a variety of the mineral corundum ( aluminium oxide). Ruby is one of the most popular traditional jewelry gems and is very durable. Other varieties of gem-quality corundum are called sapph ...
, 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.
It is considered a
best practice
A best practice is a method or technique that has been generally accepted as superior to alternatives because it tends to produce superior results. Best practices are used to achieve quality as an alternative to mandatory standards. Best practice ...
to use symbols as keys to an
associative array
In computer science, an associative array, key-value store, map, symbol table, or dictionary is an abstract data type that stores a collection of (key, value) pairs, such that each possible key appears at most once in the collection. In math ...
in Ruby.
Examples
The following is a simple example of a symbol literal in Ruby:
my_symbol = :a
my_symbol = :"an identifier"
Strings can be coerced into symbols, vice versa:
irb(main):001:0> my_symbol = "Hello, world!".intern
=> :"Hello, world!"
irb(main):002:0> my_symbol = "Hello, world!".to_sym
=> :"Hello, world!"
irb(main):003:0> my_string = :hello.to_s
=> "hello"
Symbols are objects of the
Symbol
class in Ruby:
irb(main):004:0> my_symbol = :hello_world
=> :hello_world
irb(main):005:0> my_symbol.length
=> 11
irb(main):006:0> my_symbol.class
=> Symbol
Symbols are commonly used to dynamically send messages to (call methods on) objects:
irb(main):007:0> "aoboc".split("o")
=> a", "b", "c"irb(main):008:0> "aoboc".send(:split, "o") # same result
=> a", "b", "c"
Symbols as keys of an associative array:
irb(main):009:0> my_hash =
=>
irb(main):010:0> my_hash a
=> "apple"
irb(main):011:0> my_hash b
=> "banana"
Smalltalk
In
Smalltalk
Smalltalk is a purely object oriented programming language (OOP) that was originally created in the 1970s for educational use, specifically for constructionist learning, but later found use in business. It was created at Xerox PARC by Learni ...
, 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.
[http://wiki.squeak.org/squeak/uploads/172/standard_v1_9-indexed.pdf ANSI Smalltalk standard.] In most Smalltalk implementations, selectors (method names) are implemented as symbols.
Examples
The following is a simple example of a symbol literal in Smalltalk:
my_symbol := #'an identifier' " Symbol literal "
my_symbol := #a " Technically, this is a selector literal. In most implementations, "
" selectors are symbols, so this is also a symbol literal "
Strings can be coerced into symbols, vice versa:
my_symbol := 'Hello, world!' asSymbol " => #'Hello, world!' "
my_string := #hello: asString " => 'hello:' "
Symbols conform to the
symbol
protocol, and their class is called
Symbol
in most implementations:
my_symbol := #hello_world
my_symbol class " => Symbol "
Symbols are commonly used to dynamically send messages to (call methods on) objects:
" same as 'foo' at: 2 "
'foo' perform: #at: with: 2 " => $o "
References
{{Reflist
Articles with example Ruby code
Programming constructs