Symbol (programming)

From Wikipedia, the free encyclopedia
Jump to: navigation, search

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, they are called atoms.[1]

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

Support[edit]

The following programming languages provide support for symbols:

language type name(s) example literal(s)
ANSI Common Lisp symbol, keyword 'symbol, :keyword
Clojure symbol,[2] keyword[3] 'symbol, :keyword
Elixir atom, symbol :sym
Erlang atom sym or 'sym'
Julia Symbol :sym
Objective-C SEL @selector(sym)
PICAXE BASIC symbol symbol let name = variable
Prolog atom, symbol sym
Ruby Symbol :sym or :'sym'
Scala scala.Symbol 'symbol
Scheme symbol sym
Smalltalk Symbol #sym or #'sym'
SML/NJ Atom.atom

Lisp[edit]

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

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.[5] Keyword symbols are self-evaluating[6] and interned in the package named KEYWORD.

Examples[edit]

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

Prolog[edit]

In Prolog, symbols (or atoms) are the primary primitive data types, similar to numbers.[7] The exact notation may differ in different Prolog's dialects. However, it is always quite simple (no quotations or special beginning characters are necessary).

Contrary to other languages, it is possible to give symbols some meaning by creating some Prolog's facts and/or rules.

Examples[edit]

The following example demonstrates two facts (describing what father is) and one rule (describing the meaining of sibling). These three sentences use symbols (father, zeus, hermes, perseus and sibling) and some abstract variables (X, Y and Z). The mother relationship has been omitted for clarity.

father(zeus, hermes).
father(zeus, perseus).
 
sibling(X, Y) :- father(Z, X), father(Z, Y).

Ruby[edit]

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.[8] Two symbols with the same contents will always refer to the same object.[9] It is considered a best practice to use symbols as keys to an associative array in Ruby.[8][10]

Examples[edit]

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:[11]

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

Symbols are commonly used to dynamically send messages to (call methods on) objects:

# same as "aoboc".split("o")
"aoboc".send(:split, "o") #=> ["a", "b", "c"]

Symbols as keys of an associative array:

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

Smalltalk[edit]

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.[12] In most Smalltalk implementations, selectors (method names) are implemented as symbols.

Examples[edit]

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[edit]

  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-0-9745140-5-5. 
  2. ^ Symbols on the page on Data Structures
  3. ^ Keywords on the page on Data Structures
  4. ^ Common Lisp HyperSpec, system class Symbol
  5. ^ Common Lisp HyperSpec, system class Package
  6. ^ Peter Norvig: Paradigms of Artificial Intelligence Programming: Case Studies in Common Lisp, Morgan Kaufmann, 1991, ISBN 1-55860-191-0, Web
  7. ^ Bratko, Ivan (2001). Prolog programming for artificial intelligence. Harlow, England ; New York: Addison Wesley. ISBN 0-201-40375-7. 
  8. ^ a b Kidd, Eric. "13 Ways of Looking at a Ruby Symbol". Random Hacks. Retrieved 10 July 2011. 
  9. ^ http://www.ruby-doc.org/docs/ProgrammingRuby/html/language.html#UI
  10. ^ "Using Symbols for the Wrong Reason". Gnomic Notes. 
  11. ^ "Symbol". Ruby Documentation. Retrieved 10 July 2011. 
  12. ^ http://wiki.squeak.org/squeak/uploads/172/standard_v1_9-indexed.pdf ANSI Smalltalk standard.