Symbol (programming): Difference between revisions

Content deleted Content added
WP:LINKs: plural > WP:SINGULAR, adds, update-standardizes, needless WP:PIPEs > WP:NOPIPEs. Small WP:COPYEDITs WP:EoS: clarify, WP:TERSE. WP:REFerence WP:CITation parameters: update-standardizes, respaces, adds, fills, author > last + first, recorders, conform to master templates. Cut needless carriage return in paragraph.
Line 1:
{{Short description|Datatype in programming}}
A '''symbol''' in [[computer programming]] is a [[Primitive data type|primitive]] [[data type]] whosewhich [[Instance (computer science)|instances]] have a unique human-readable form. Symbols can be used as [[Identifier (computer languages)|identifier]]s. In some [[programming languageslanguage]]s, they are called '''atoms'''.<ref name=pickaxe>{{cite book |lastlast1=HuntThomas |firstfirst1=Dave |author1-link=Dave Thomas ;(programmer) |last2=Fowler |first2=Chad Fowler|last3=Hunt ;|first3=Andy |author3-link=Andy Hunt (author) |year=2001 |title=Programming Ruby the pragmatic programmers' guide ; [includes Ruby 1.8]|year=2001 |publisher=The Pragmatic Bookshelf |___location=Raleigh, NCNorth Carolina |isbn=978-0-9745140-5-5 |url=https://archive.org/details/programmingrubyp00thom |edition=2. ed.2nd, 10. print. |url-access=registration}}</ref> Uniqueness is enforced by holding them in a [[symbol table]]. The most common use of symbols by programmers is forto performingperform language [[reflectionReflective (programming) | reflection]] (particularly for [[callbackCallback (computer programming)|callbacks]]), and most common indirectly is their use to create object [[linkageLinkage (software)|linkages]].
 
In the most trivial [[implementation]], they are essentially named [[integer]]s; (e.g., the [[enumerated type]] in [[C (programming language)|C]] language.
 
==Support==
Line 25:
| [[Julia (programming language)|Julia]] || Symbol || <code>:sym</code>
|-
| [[K (programming language)|K]] || symbol || `sym
|-
| [[Objective-C]] || SEL || <code>@selector(sym)</code>
Line 33:
| [[PostScript]] || name || <code>/sym</code> or <code>sym</code>
|-
| [[Prolog (programming language)|Prolog]] || atom, symbol || <code>sym</code> or <code>'sym'</code>
|-
| [[Ruby (programming language)|Ruby]] || Symbol || <code>:sym</code> or <code>:'sym'</code>
Line 96:
 
===Prolog===
In [[Prolog (programming language)|Prolog]], symbols (or atoms) are the primarymain primitive data types, similar to numbers.<ref name=Bratko2001>{{Cite book | last1 last= Bratko | first1 first= Ivan |year=2001 |title = Prolog programming for artificial intelligence | year = 2001 | publisher = Addison Wesley | ___location = Harlow, England ; New York | isbn = 978-0-201-40375-6 }}</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 many other languages, it is possible to give symbols somea ''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 beenis omitted for clarity.
<syntaxhighlight lang="prolog">
father(zeus, hermes).
Line 111:
===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 |date=20 January 2007 |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 |access-date=10 July 2011}}</ref> Two symbols with the same contents will always refer to the same object.<ref>{{Cite web | url=http://www.ruby-doc.org/docs/ProgrammingRuby/html/language.html#UI | title=Programming Ruby: The Pragmatic Programmer's Guide}}</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>
 
Line 156:
 
===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.
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====
Line 183 ⟶ 182:
 
==References==
{{Reflist}}
<references/>
 
[[Category:Articles with example Ruby code]]