Symbol (programming)

{{generalize|date=October 2023}}{{Short description|Datatype in programming}}

A symbol in computer programming is a primitive data type whose instances have a human-readable form. Symbols can be used as identifiers. In some programming languages, they are called atoms.{{cite book |last1=Thomas |first1=Dave |author1-link=Dave Thomas (programmer) |last2=Fowler |first2=Chad |last3=Hunt |first3=Andy |author3-link=Andy Hunt (author) |year=2001 |title=Programming Ruby the pragmatic programmers' guide; [includes Ruby 1.8] |publisher=The Pragmatic Bookshelf |location=Raleigh, North Carolina |isbn=978-0-9745140-5-5 |url=https://archive.org/detail/programmingrubyp00thom |edition=2nd, 10 print. |url-access=registration}} Uniqueness is enforced by holding them in a symbol table. The most common use of symbols by programmers is to perform language reflection (particularly for callbacks), and the most common indirectly is their use to create object linkages.

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

Support

The following programming languages provide runtime support for symbols:

{{dynamic list}}

class="wikitable sortable"
language || type name(s) || example literal(s)
ANSI Common Lispsymbol, keywordsymbol, :keyword
Clojuresymbol,[http://clojure.org/data_structures#Data%20Structures-Symbols Symbols] on the page on Data Structures keyword[http://clojure.org/data_structures#Data%20Structures-Keywords Keywords] on the page on Data Structures'symbol, :keyword
DartSymbol{{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}}#sym
Elixiratom, symbol:sym
Erlangatomsym or 'sym'
JavaScript (ES6 and later)SymbolSymbol("sym");
JuliaSymbol:sym
Ksymbol`sym
Objective-CSEL@selector(sym)
PICAXE BASICsymbol symbol let name = variable
PostScriptname/sym or sym
Prologatom, symbolsym or 'sym'
RubySymbol:sym or :'sym'
Scalascala.Symbol'symbol
Schemesymbolsym
SmalltalkSymbol#sym or #'sym'
SML/NJAtom.atom
Wolfram LanguageSymbolSymbol["sym"] or sym

=Julia=

Symbols in Julia are interned strings used to represent identifiers in parsed Julia code(ASTs) and as names or labels to identify entities (for example as keys in a dictionary).{{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}}

=Lisp=

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.{{Cite web|url=http://www.lispworks.com/documentation/HyperSpec/Body/t_symbol.htm|title=CLHS: System Class SYMBOL|website=www.lispworks.com}}

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 not 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.{{Cite web|url=http://www.lispworks.com/documentation/HyperSpec/Body/t_pkg.htm#package|title=CLHS: System Class PACKAGE|website=www.lispworks.com}} Keyword symbols are self-evaluating,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] and interned in the package named KEYWORD.

==Examples==

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

Symbols, which are not interned in a package, can also be created and have a notation:

  1. :uninterned-symbol

=PostScript=

In PostScript, references to name objects can be either literal or executable, influencing the behaviour of the interpreter when encountering them. The cvx and cvl operators can be used to convert between the two forms. When names are constructed from strings by means of the cvn operator, the set of allowed characters is unrestricted.

=Prolog=

In Prolog, symbols (or atoms) are the main primitive data types, similar to numbers.{{Cite book |last=Bratko |first=Ivan |year=2001 |title=Prolog programming for artificial intelligence |publisher=Addison Wesley |location=Harlow, England; New York |isbn=978-0-201-40375-6}} The exact notation may differ in different Prolog 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 a meaning by creating some Prolog 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 is omitted for clarity.

father(zeus, hermes).

father(zeus, perseus).

sibling(X, Y) :- father(Z, X), father(Z, Y).

=Ruby=

In Ruby, symbols can be created with a literal form, or by converting a string.

They can be used as an identifier or an interned string.{{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}} Two symbols with the same contents will always refer to the same object.{{Cite web|url=http://ruby-doc.com/docs/ProgrammingRuby/|title=Programming Ruby: The Pragmatic Programmer's Guide|website=ruby-doc.com}}

It is considered a best practice to use symbols as keys to an associative array in Ruby.{{cite web|title=Using Symbols for the Wrong Reason|url=http://microjet.ath.cx/WebWiki/2005.12.27_UsingSymbolsForTheWrongReason.html|work=Gnomic Notes}}

==Examples==

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

my_symbol = :a

my_symbol = :"an identifier"

Strings can be coerced into symbols, vice versa:

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"

Symbols are objects of the Symbol class in Ruby:{{cite web|title=Symbol|url=http://www.ruby-doc.org/core/classes/Symbol.html|work=Ruby Documentation|access-date=10 July 2011}}

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

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

irb(main):007:0> "aoboc".split("o")

=> ["a", "b", "c"]

irb(main):008:0> "aoboc".send(:split, "o") # same result

=> ["a", "b", "c"]

Symbols as keys of an associative array:

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"

=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.http://wiki.squeak.org/squeak/uploads/172/standard_v1_9-indexed.pdf ANSI Smalltalk standard. In most Smalltalk implementations, selectors (method names) are implemented as symbols.

==Examples==

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