Symbol (programming)

Last updated

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

Contents

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:

languagetype name(s)example literal(s)
ANSI Common Lisp symbol, keywordsymbol, :keyword
Clojure symbol, [2] keyword [3] 'symbol, :keyword
Dart Symbol [4] #sym
Elixir atom, symbol:sym
Erlang atomsym or 'sym'
JavaScript (ES6 and later)SymbolSymbol("sym");
Julia Symbol:sym
K symbol`sym
Objective-C SEL@selector(sym)
PICAXE BASIC symbol symbol let name = variable
PostScript name/sym or sym
Prolog atom, symbolsym or 'sym'
Ruby Symbol:sym or :'sym'
Scala scala.Symbol'symbol
Scheme symbolsym
Smalltalk Symbol#sym or #'sym'
SML/NJ Atom.atom
Wolfram Language SymbolSymbol["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). [5]

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

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. [7] Keyword symbols are self-evaluating, [8] 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:

#: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. [9] 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. [1] They can be used as an identifier or an interned string. [10] Two symbols with the same contents will always refer to the same object. [11] It is considered a best practice to use symbols as keys to an associative array in Ruby. [10] [12]

Examples

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

my_symbol=:amy_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: [13]

irb(main):004:0> my_symbol=:hello_world=> :hello_worldirb(main):005:0> my_symbol.length=> 11irb(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. [14] 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_worldmy_symbolclass" => 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 "

Related Research Articles

<span class="mw-page-title-main">Common Lisp</span> Programming language standard

Common Lisp (CL) is a dialect of the Lisp programming language, published in American National Standards Institute (ANSI) standard document ANSI INCITS 226-1994 (S2018). The Common Lisp HyperSpec, a hyperlinked HTML version, has been derived from the ANSI Common Lisp standard.

<span class="mw-page-title-main">Dylan (programming language)</span> Multi-paradigm programming language

Dylan is a multi-paradigm programming language that includes support for functional and object-oriented programming (OOP), and is dynamic and reflective while providing a programming model designed to support generating efficient machine code, including fine-grained control over dynamic and static behaviors. It was created in the early 1990s by a group led by Apple Computer.

A "Hello, World!" program is generally a simple computer program which outputs to the screen a message similar to "Hello, World!" while ignoring any user input. A small piece of code in most general-purpose programming languages, this program is used to illustrate a language's basic syntax. A "Hello, World!" program is often the first written by a student of a new programming language, but such a program can also be used as a sanity check to ensure that the computer software intended to compile or run source code is correctly installed, and that its operator understands how to use it.

<span class="mw-page-title-main">Ruby (programming language)</span> General-purpose programming language

Ruby is an interpreted, high-level, general-purpose programming language. It was designed with an emphasis on programming productivity and simplicity. In Ruby, everything is an object, including primitive data types. It was developed in the mid-1990s by Yukihiro "Matz" Matsumoto in Japan.

<span class="mw-page-title-main">Smalltalk</span> Object-oriented programming language released first in 1972

Smalltalk is a purely object oriented programming language (OOP) that was originally created in the 1970s for educational use, specifically for constructionist learning, but later found use in business. It was created at Xerox PARC by Learning Research Group (LRG) scientists, including Alan Kay, Dan Ingalls, Adele Goldberg, Ted Kaehler, Diana Merry, and Scott Wallace.

In computing, serialization is the process of translating a data structure or object state into a format that can be stored or transmitted and reconstructed later. When the resulting series of bits is reread according to the serialization format, it can be used to create a semantically identical clone of the original object. For many complex objects, such as those that make extensive use of references, this process is not straightforward. Serialization of objects does not include any of their associated methods with which they were previously linked.

In a computer language, a reserved word is a word that cannot be used as an identifier, such as the name of a variable, function, or label – it is "reserved from use". This is a syntactic definition, and a reserved word may have no user-defined meaning.

<span class="mw-page-title-main">S-expression</span> Data serialization format

In computer programming, an S-expression is an expression in a like-named notation for nested list (tree-structured) data. S-expressions were invented for and popularized by the programming language Lisp, which uses them for source code as well as data.

In computing, type introspection is the ability of a program to examine the type or properties of an object at runtime. Some programming languages possess this capability.

In computer programming, a function object is a construct allowing an object to be invoked or called as if it were an ordinary function, usually with the same syntax. In some languages, particularly C++, function objects are often called functors.

In computing, a here document is a file literal or input stream literal: it is a section of a source code file that is treated as if it were a separate file. The term is also used for a form of multiline string literals that use similar syntax, preserving line breaks and other whitespace in the text.

In computer programming, a sigil is a symbol affixed to a variable name, showing the variable's datatype or scope, usually a prefix, as in $foo, where $ is the sigil.

In computer programming, an entry point is the place in a program where the execution of a program begins, and where the program has access to command line arguments.

C++/CLI is a variant of the C++ programming language, modified for Common Language Infrastructure. It has been part of Visual Studio 2005 and later, and provides interoperability with other .NET languages such as C#. Microsoft created C++/CLI to supersede Managed Extensions for C++. In December 2005, Ecma International published C++/CLI specifications as the ECMA-372 standard.

<span class="mw-page-title-main">Syntax (programming languages)</span> Set of rules defining correctly structured programs

In computer science, the syntax of a computer language is the rules that define the combinations of symbols that are considered to be correctly structured statements or expressions in that language. This applies both to programming languages, where the document represents source code, and to markup languages, where the document represents data.

String functions are used in computer programming languages to manipulate a string or query information about a string.

This comparison of programming languages compares the features of language syntax (format) for over 50 computer programming languages.

The structure of the Perl programming language encompasses both the syntactical rules of the language and the general ways in which programs are organized. Perl's design philosophy is expressed in the commonly cited motto "there's more than one way to do it". As a multi-paradigm, dynamically typed language, Perl allows a great degree of flexibility in program design. Perl also encourages modularization; this has been attributed to the component-based design structure of its Unix roots, and is responsible for the size of the CPAN archive, a community-maintained repository of more than 100,000 modules.

In computer programming languages, an identifier is a lexical token that names the language's entities. Some of the kinds of entities an identifier might denote include variables, data types, labels, subroutines, and modules.

References

  1. 1 2 3 Thomas, Dave; Fowler, Chad; Hunt, Andy (2001). Programming Ruby the pragmatic programmers' guide; [includes Ruby 1.8] (2nd, 10 print. ed.). Raleigh, North Carolina: 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. "A tour of the Dart language | Symbols". Dart programming language. Retrieved 17 January 2021.
  5. "Julia Core.Symbol". Julia Documentation. Retrieved 31 May 2022.
  6. "CLHS: System Class SYMBOL". www.lispworks.com.
  7. "CLHS: System Class PACKAGE". www.lispworks.com.
  8. Peter Norvig: Paradigms of Artificial Intelligence Programming: Case Studies in Common Lisp, Morgan Kaufmann, 1991, ISBN   1-55860-191-0, Web
  9. Bratko, Ivan (2001). Prolog programming for artificial intelligence. Harlow, England; New York: Addison Wesley. ISBN   978-0-201-40375-6.
  10. 1 2 Kidd, Eric (20 January 2007). "13 Ways of Looking at a Ruby Symbol". Random Hacks. Retrieved 10 July 2011.
  11. "Programming Ruby: The Pragmatic Programmer's Guide". ruby-doc.com.
  12. "Using Symbols for the Wrong Reason". Gnomic Notes.
  13. "Symbol". Ruby Documentation. Retrieved 10 July 2011.
  14. http://wiki.squeak.org/squeak/uploads/172/standard_v1_9-indexed.pdf ANSI Smalltalk standard.