Content deleted Content added
←Replaced content with '""abort"" ""abort""' Tags: Replaced blanking |
m Reverted edits by 2001:8004:18EE:239A:B42B:CDD3:3854:2082 (talk) to last version by HuggaBounce |
||
Line 1:
{{distinguish|Symbol (computing)}}
A '''symbol''' in [[computer programming]] is a [[primitive data type]] whose [[Instance (computer science)|instances]] have a unique human-readable form. Symbols can be used as [[identifier]]s. In some [[programming languages]], they are called '''atoms'''.<ref name=pickaxe>{{cite book|last=Hunt|first=Dave Thomas ; Chad Fowler ; Andy|title=Programming Ruby the pragmatic programmers' guide ; [includes Ruby 1.8]|year=2001|publisher=The Pragmatic Bookshelf|___location=Raleigh, NC|isbn=978-0-9745140-5-5|url=http://www.ruby-doc.org/docs/ProgrammingRuby/html/language.html|edition=2. ed., 10. print.}}</ref> Uniqueness is enforced by holding them in a [[symbol table]]. The most common use of symbols by programmers is for performing language [[reflection (programming) | reflection]] (particularly for [[callback (computer programming)|callbacks]]), and most common indirectly is their use to create object [[linkage (software)|linkages]].
In the most trivial [[implementation]], they are essentially named [[integer]]s (e.g. the [[enumerated type]] in C).
==Support==
The following [[programming language]]s provide [[Runtime system|runtime]] support for symbols:
{{dynamic list}}
{| class="wikitable sortable"
|-
! language || type name(s) || example literal(s)
|-
| [[ANSI]] [[Common Lisp]] || symbol, keyword || <tt>symbol</tt>, <tt>:keyword</tt>
|-
| [[Clojure]] || symbol,<ref>[http://clojure.org/data_structures#Data%20Structures-Symbols Symbols] on the page on Data Structures</ref> keyword<ref>[http://clojure.org/data_structures#Data%20Structures-Keywords Keywords] on the page on Data Structures</ref> || <tt>'symbol</tt>, <tt>:keyword</tt>
|-
| [[Elixir (programming language)|Elixir]] || atom, symbol || <tt>:sym</tt>
|-
| [[Erlang (programming language)|Erlang]] || atom || <tt>sym</tt> or <tt>'sym'</tt>
|-
| [[Julia (programming language)|Julia]] || Symbol || <tt>:sym</tt>
|-
| [[Objective-C]] || SEL || <tt>@selector(sym)</tt>
|-
| [[PICAXE]] [[BASIC]] || symbol || <tt> symbol let name = variable </tt>
|-
| [[Prolog (programming language)|Prolog]] || atom, symbol || <tt>sym</tt>
|-
| [[Ruby (programming language)|Ruby]] || Symbol || <tt>:sym</tt> or <tt>:'sym'</tt>
|-
| [[Scala (programming language)|Scala]] || scala.Symbol || <tt>'symbol</tt>
|-
| [[Scheme (programming language)|Scheme]] || symbol || <tt>sym</tt>
|-
| [[Smalltalk]] || Symbol || <tt>#sym</tt> or <tt>#'sym'</tt>
|-
| [[SML/NJ]] || Atom.atom ||
|-
| [[JavaScript]](ES6) || Symbol || <tt>Symbol("sym");</tt>
|-
| [[Wolfram Language]] || Symbol || <tt>Symbol["sym"]</tt> or <tt>sym</tt>
|-
|[[K (programming language)]]
|symbol
|`sym
|}
===Lisp===
A symbol in [[Lisp (programming language)|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.<ref>[http://www.lispworks.com/documentation/HyperSpec/Body/t_symbol.htm Common Lisp HyperSpec, system class Symbol]</ref>
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't 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.<ref>[http://www.lispworks.com/documentation/HyperSpec/Body/t_pkg.htm#package Common Lisp HyperSpec, system class Package]</ref> Keyword symbols are self-evaluating<ref>[[Peter Norvig]]: ''Paradigms of Artificial Intelligence Programming: Case Studies in Common Lisp'', Morgan Kaufmann, 1991, {{ISBN|1-55860-191-0}}, [http://norvig.com/paip.html Web]</ref> and interned in the package named KEYWORD.
====Examples====
The following is a simple external representation of a [[Common Lisp]] symbol:
<source lang="lisp">
this-is-a-symbol
</source>
Symbols can contain whitespace (and all other characters):
<source lang="lisp">
|This is a symbol with whitespace|
</source>
In Common Lisp symbols with a leading colon in their printed representations are [[Keyword (computer programming)|keyword symbols]]. These are interned in the keyword package.
<source lang="lisp">
:keyword-symbol
</source>
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.
<source lang="lisp">
package-name::symbol-name
</source>
Packages can export symbols. Then only one colon is written between the name of the package and the name of the symbol.
<source lang="lisp">
package:exported-symbol
</source>
Symbols, which are not interned in a package, can also be created and have a notation:
<source lang="lisp">
#:uninterned-symbol
</source>
===Prolog===
In [[Prolog (programming language)|Prolog]], symbols (or atoms) are the primary primitive data types, similar to numbers.<ref name=Bratko2001>{{Cite book | last1 = Bratko | first1 = Ivan | title = Prolog programming for artificial intelligence | year = 2001 | publisher = Addison Wesley | ___location = Harlow, England ; New York | isbn = 0-201-40375-7 | pages = }}</ref> 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====
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 has been omitted for clarity.
<source lang="prolog">
father(zeus, hermes).
father(zeus, perseus).
sibling(X, Y) :- father(Z, X), father(Z, Y).
</source>
===Ruby===
In [[Ruby (programming language)|Ruby]], symbols can be created with a literal form, or by converting a string.<ref name=pickaxe />
They can be used as an identifier or an interned string.<ref name="rubysymbol">{{cite web|last=Kidd|first=Eric|title=13 Ways of Looking at a Ruby Symbol|url=http://www.randomhacks.net/articles/2007/01/20/13-ways-of-looking-at-a-ruby-symbol#9|work=Random Hacks|accessdate=10 July 2011}}</ref> Two symbols with the same contents will always refer to the same object.<ref>http://www.ruby-doc.org/docs/ProgrammingRuby/html/language.html#UI</ref>
It is considered a [[best practice]] to use symbols as keys to an [[associative array]] in Ruby.<ref name=rubysymbol /><ref name="wrongreason">{{cite web|title=Using Symbols for the Wrong Reason|url=http://microjet.ath.cx/WebWiki/2005.12.27_UsingSymbolsForTheWrongReason.html|work=Gnomic Notes}}</ref>
====Examples====
The following is a simple example of a symbol literal in Ruby:<ref name=pickaxe />
<source lang=ruby>
my_symbol = :a
my_symbol = :"an identifier"
</source>
Strings can be coerced into symbols, vice versa:
<source lang=irb>
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"
</source>
Symbols are objects of the <code>Symbol</code> class in Ruby:<ref name="rdocsymbol">{{cite web|title=Symbol|url=http://www.ruby-doc.org/core/classes/Symbol.html|work=Ruby Documentation|accessdate=10 July 2011}}</ref>
<source lang=irb>
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
</source>
Symbols are commonly used to dynamically send messages to (call methods on) objects:
<source lang=irb>
irb(main):007:0> "aoboc".split("o")
=> ["a", "b", "c"]
irb(main):008:0> "aoboc".send(:split, "o") # same result
=> ["a", "b", "c"]
</source>
Symbols as keys of an associative array:
<source lang=irb>
irb(main):009:0> my_hash = { a: "apple", b: "banana" }
=> {:a=>"apple", :b=>"banana"}
irb(main):010:0> my_hash[:a]
=> "apple"
irb(main):011:0> my_hash[:b]
=> "banana"
</source>
===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.<ref>http://wiki.squeak.org/squeak/uploads/172/standard_v1_9-indexed.pdf ANSI Smalltalk standard.</ref> In most Smalltalk implementations, selectors (method names) are implemented as symbols.
====Examples====
The following is a simple example of a symbol literal in Smalltalk:
<source lang=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 "
</source>
Strings can be coerced into symbols, vice versa:
<source lang=smalltalk>
my_symbol := 'Hello, world!' asSymbol " => #'Hello, world!' "
my_string := #hello: asString " => 'hello:' "
</source>
Symbols conform to the <code>symbol</code> protocol, and their class is called <code>Symbol</code> in most implementations:
<source lang=smalltalk>
my_symbol := #hello_world
my_symbol class " => Symbol "
</source>
Symbols are commonly used to dynamically send messages to (call methods on) objects:
<source lang=smalltalk>
" same as 'foo' at: 2 "
'foo' perform: #at: with: 2 " => $o "
</source>
==References==
<references/>
[[Category:Articles with example Ruby code]]
[[Category:Programming constructs]]
|