Symbol (programming)

This is an old revision of this page, as edited by 208.80.119.67 (talk) at 21:23, 6 September 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 some programming languages, that are called atoms[1].

In the most trivial implementation, they are essentially named integers (e.g. the enumerated type in C).

Support

The following programming languages provide support for symbols:

language type name(s) example literal(s)
ANSI Common Lisp symbol, keyword sym, :key
R6RS Scheme symbol 'sym
Ruby Symbol :sym
Ericsson Erlang atom 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. [2].

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[3]. 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

Ruby

In Ruby, symbols can be created with a literal form, or by converting a string[1]. They can be used as an identifier or an interned string[4]. It is considered a best practice to use symbols as keys to an associative array in Ruby[4][5].

Examples

The following is a simple example of a symbol literal in Ruby[1]:

my_symbol = :a
my_symbol = :"an identifier"

Strings can be coerced into symbols, vice versa:

my_symbol = "Hello, world!".intern #=> :"Hello, world!"
my_symbol = "Hello, world!".to_sym #=> :"Hello, world!"
my_string = :hello.to_s

Symbols are objects of the Symbol class in Ruby[6]:

my_symbol = :hello_world
my_symbol.length #=> 11
my_symbol.class #=> Symbol

Symbols as keys of an associative array:

my_hash = { a: "apple", b: "banana" }
my_hash[:a] #=> "apple"
my_hash[:b] #=> "banana"

References

  1. ^ a b c Hunt, Dave Thomas ; Chad Fowler ; Andy (2001). Programming Ruby the pragmatic programmers' guide ; [includes Ruby 1.8] (2. ed., 10. print. ed.). Raleigh, NC: The Pragmatic Bookshelf. ISBN 978-0974514055.{{cite book}}: CS1 maint: multiple names: authors list (link)
  2. ^ Common Lisp HyperSpec, system class Symbol
  3. ^ Common Lisp HyperSpec, system class Package
  4. ^ a b Kidd, Eric. "13 Ways of Looking at a Ruby Symbol". Random Hacks. Retrieved 10 July 2011.
  5. ^ "Using Symbols for the Wrong Reason". Gnomic Notes.
  6. ^ "Symbol". Ruby Documentation. Retrieved 10 July 2011.