Symbol (programming)

This is an old revision of this page, as edited by 63.170.100.98 (talk) at 19:21, 7 April 2011 (Support). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

A symbol in computer programming is a primitive datatype whose instances have a unique human-readable form. Symbols can be used as identifiers.

In the most trivial implementation, they are essentially named integers.

Support

The following programming languages provide support for symbols:

language type name(s) example literal(s)
ANSI Common Lisp symbol, keyword sym, :key
Ruby (programming language) Symbol :sym
ECMA JavaScript ? sym
Ericsson Erlang ? sym


Lisp

A symbol in Lisp is unique in a namespace (called 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. [1].

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[2]. Keyword symbols are interned in the package named KEYWORD and keyword symbols evaluate to themselves.

Examples

The following is a simple external representation of Common Lisp 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

References