Symbol (programming)


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. Uniqueness is enforced by holding them in a symbol table. The most common use of symbols by programmers is to perform language reflection, 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.

Support

The following programming languages provide runtime support for symbols:
languagetype nameexample literal
ANSI Common Lispsymbol, keywordsymbol, :keyword
Clojuresymbol, keywordsymbol, :keyword
DartSymbol#sym
Elixiratom, symbol:sym
Erlangatomsym or 'sym'
JavaScript SymbolSymbol;
JuliaSymbol:sym
Ksymbol`sym
Objective-CSEL@selector
PICAXE BASICsymbol symbol let name = variable
PostScriptname/sym or sym
Prologatom, symbolsym or 'sym'
RubySymbol:sym or :'sym'
Scalascala.Symbol'symbol
Schemesymbolsymbol
SmalltalkSymbol#sym or #'sym'
SML/NJAtom.atom
Wolfram LanguageSymbolSymbol or sym

Julia

Symbols in Julia are interned strings used to represent identifiers in parsed Julia code and as names or labels to identify entities.

Lisp

A symbol in Lisp is unique in a namespace. 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 symbol:

this-is-a-symbol

Symbols can contain whitespace :


In Common Lisp symbols with a leading colon in their printed representations are keyword symbols. These are interned in the keyword package.


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:

  1. :uninterned-symbol

PostScript

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.

Prolog

In Prolog, symbols are the main primitive data types, similar to numbers. The exact notation may differ in different Prolog dialects. However, it is always quite simple.
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 and one rule. These three sentences use symbols and some abstract variables. The mother relationship is omitted for clarity.

father.
father.
sibling :- father, father.

Ruby

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. Two symbols with the same contents will always refer to the same object.
It is considered a best practice to use symbols as keys to an associative array 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:001:0> my_symbol = "Hello, world!".intern
=> :"Hello, world!"
irb:002:0> my_symbol = "Hello, world!".to_sym
=> :"Hello, world!"
irb:003:0> my_string = :hello.to_s
=> "hello"

Symbols are objects of the Symbol class in Ruby:

irb:004:0> my_symbol = :hello_world
=> :hello_world
irb:005:0> my_symbol.length
=> 11
irb:006:0> my_symbol.class
=> Symbol

Symbols are commonly used to dynamically send messages to objects:

irb:007:0> "aoboc".split
=>
irb:008:0> "aoboc".send # same result
=>

Symbols as keys of an associative array:

irb:009:0> my_hash =
=>
irb:010:0> my_hash
=> "apple"
irb:011:0> my_hash
=> "banana"

Smalltalk

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. In most Smalltalk implementations, selectors 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 objects:

" same as 'foo' at: 2 "
'foo' perform: #at: with: 2 " => $o "