Content deleted Content added
m Task 70: Update syntaxhighlight tags - remove use of deprecated <source> tags |
Chemophilic (talk | contribs) Tags: Mobile edit Mobile web edit Advanced mobile edit |
||
(28 intermediate revisions by 22 users not shown) | |||
Line 1:
{{generalize|date=October 2023}}{{Short description|Datatype in programming}}
A '''symbol''' in [[computer programming]] is a [[Primitive data type|primitive]] [[data type]] whose [[Instance (computer science)|instances]] have a
In the most trivial [[implementation]], they are essentially named [[integer]]s;
==Support==
Line 10 ⟶ 11:
! language || type name(s) || example literal(s)
|-
| [[ANSI]] [[Common Lisp]] || symbol, keyword || <
|-
| [[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> || <
|-
| [[Dart (programming language)|Dart]] || Symbol<ref>{{cite web |title=A tour of the Dart language {{pipe}} Symbols |url=https://dart.dev/guides/language/language-tour#symbols |website=Dart programming language |access-date=17 January 2021}}</ref> || <code>#sym</code>
| [[Elixir (programming language)|Elixir]] || atom, symbol || <tt>:sym</tt>▼
|-
| [[
|-
| [[
|-
| [[
|-
| [[
|-
| [[
|-
| [[
|-
| [[
|-
| [[
|-
| [[
|-
| [[Ruby (programming language)|Ruby]] || Symbol || <code>:sym</code> or <code>:'sym'</code>
| [[SML/NJ]] || Atom.atom ||▼
|-
| [[
|-
▲| [[
| [[Wolfram Language]] || Symbol || <tt>Symbol["sym"]</tt> or <tt>sym</tt>▼
|-
| [[Smalltalk]] || Symbol || <code>#sym</code> or <code>#'sym'</code>
|-
▲| [[SML/NJ]] || Atom.atom ||
|-
|}
===Julia===
Symbols in [[Julia (programming language)|Julia]] are [[String interning|interned strings]] used to represent identifiers in parsed Julia code([[Abstract syntax tree|ASTs]]) and as names or labels to identify entities (for example as keys in a dictionary).<ref>{{cite web |title=Julia Core.Symbol |url=https://docs.julialang.org/en/v1/base/base/#Core.Symbol |website=Julia Documentation |access-date=31 May 2022}}</ref>
===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>
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
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>
====Examples====
Line 85 ⟶ 91:
#:uninterned-symbol
</syntaxhighlight>
===PostScript===
In [[PostScript]], references to ''name'' objects can be either ''literal'' or ''executable'', influencing the behaviour of the interpreter when encountering them. The <code>cvx</code> and <code>cvl</code> operators can be used to convert between the two forms. When names are constructed from strings by means of the <code>cvn</code> operator, the set of allowed characters is unrestricted.
===Prolog===
In [[
Contrary to many other languages, it is possible to give symbols
====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
<syntaxhighlight lang="prolog">
father(zeus, hermes).
Line 102 ⟶ 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 |
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 120 ⟶ 129:
=> "hello"
</syntaxhighlight>
Symbols are objects of the <code>Symbol</code> class in Ruby:<ref name="rdocsymbol">{{cite web|title=Symbol|url=
<syntaxhighlight lang=irb>
irb(main):004:0> my_symbol = :hello_world
Line 147 ⟶ 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.
====Examples====
Line 174 ⟶ 182:
==References==
{{Reflist}}
[[Category:Articles with example Ruby code]]
|