Movatterモバイル変換


[0]ホーム

URL:


Jump to content
WikipediaThe Free Encyclopedia
Search

Symbol (programming)

From Wikipedia, the free encyclopedia
This articlefocuses only on one specialised aspect of its subject. Relevant discussion may be found on thetalk page. Please helpimprove it by adding more general information.(October 2023) (Learn how and when to remove this message)
Datatype in programming

Asymbol incomputer programming is aprimitivedata type whoseinstances have a human-readable form. Symbols can be used asidentifiers. In someprogramming languages, they are calledatoms.[1] Uniqueness is enforced by holding them in asymbol table. The most common use of symbols by programmers is to perform languagereflection (particularly forcallbacks), and the most common indirectly is their use to create objectlinkages.

In the most trivialimplementation, they are essentially namedintegers; e.g., theenumerated type inC language.

Support

[edit]

The followingprogramming languages provideruntime support for symbols:

This is adynamic list and may never be able to satisfy particular standards for completeness. You can help byediting the page to add missing items, with references toreliable sources.
languagetype name(s)example literal(s)
ANSICommon Lispsymbol, keywordsymbol,:keyword
Clojuresymbol,[2] keyword[3]symbol,:keyword
DartSymbol[4]#sym
Elixiratom, symbol:sym
Erlangatomsym or'sym'
JavaScript (ES6 and later)SymbolSymbol("sym");
JuliaSymbol:sym
Ksymbol`sym
Objective-CSEL@selector(sym)
PICAXEBASICsymbol symbol let name = variable
PostScriptname/sym orsym
Prologatom, symbolsym or'sym'
RubySymbol:sym or:'sym'
Scalascala.Symbol'symbol
Schemesymbolsymbol
SmalltalkSymbol#sym or#'sym'
SML/NJAtom.atom
Wolfram LanguageSymbolSymbol["sym"] orsym

Julia

[edit]

Symbols inJulia areinterned 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

[edit]

A symbol inLisp is unique in anamespace (orpackage inCommon 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 notinterned, 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

[edit]

The following is a simple external representation of aCommon 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 arekeyword 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

[edit]

InPostScript, references toname objects can be eitherliteral orexecutable, influencing the behaviour of the interpreter when encountering them. Thecvx andcvl operators can be used to convert between the two forms. When names are constructed from strings by means of thecvn operator, the set of allowed characters is unrestricted.

Prolog

[edit]

InProlog, 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 ameaning by creating some Prolog facts and/or rules.

Examples

[edit]

The following example demonstrates two facts (describing whatfather is) and one rule (describing themeaning ofsibling). These three sentences use symbols (father, zeus, hermes, perseus and sibling) and some abstract variables (X, Y and Z). Themother relationship is omitted for clarity.

father(zeus,hermes).father(zeus,perseus).sibling(X,Y):-father(Z,X),father(Z,Y).

Ruby

[edit]

InRuby, 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 abest practice to use symbols as keys to anassociative array in Ruby.[10][12]

Examples

[edit]

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 theSymbol 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

[edit]

InSmalltalk, 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

[edit]

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 thesymbol protocol, and their class is calledSymbol 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 "

References

[edit]
  1. ^abcThomas, 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. Retrieved17 January 2021.
  5. ^"Julia Core.Symbol".Julia Documentation. Retrieved31 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. ^abKidd, Eric (20 January 2007)."13 Ways of Looking at a Ruby Symbol".Random Hacks. Retrieved10 July 2011.
  11. ^"Programming Ruby: The Pragmatic Programmer's Guide".
  12. ^"Using Symbols for the Wrong Reason".Gnomic Notes.
  13. ^"Symbol".Ruby Documentation. Retrieved10 July 2011.
  14. ^http://wiki.squeak.org/squeak/uploads/172/standard_v1_9-indexed.pdf ANSI Smalltalk standard.
Retrieved from "https://en.wikipedia.org/w/index.php?title=Symbol_(programming)&oldid=1310952715"
Category:
Hidden categories:

[8]ページ先頭

©2009-2026 Movatter.jp