Skip to content

Symbol

Status: Stable

documented, exercised by the test suite and/or worked examples, with no known limitations recorded.

Description

Symbol["name"]

refers to the symbol with the specified name, creating it in $Context if none yet exists.

Notes All symbols, whether explicitly entered using Symbol or not, have head Symbol; x\_Symbol matches any symbol. The name string may contain letters, letter-like forms, or digits but must not start with a digit. A backtick (\`) separates context prefixes; a leading backtick makes the name relative to the current context $Context. Attributes: Protected.

Examples (8)

Every input below was run against the current Mathilda build and its output recorded.

Basic examples (4)

In[1]:= Symbol["x"]
Out[1]= x

In[2]:= Head[%]
Out[2]= Out

In[3]:= {f[x], f["x"], f[2]} /. f[s_Symbol] :> g[s]
Out[3]= {g[x], f["x"], f[2]}

In[4]:= Symbol["a`x"]
Out[4]= a`x

Applications (4)

In[5]:= Symbol["x"]
Out[5]= x

The result is a genuine symbol

In[6]:= Head[Symbol["abc"]]
Out[6]= Symbol

An embedded backtick gives an absolutely-qualified name

In[7]:= Symbol["a`x"]
Out[7]= a`x

X_Symbol matches only the symbol

In[8]:= {f[x], f["x"], f[2]} /. f[s_Symbol] :> g[s]
Out[8]= {g[x], f["x"], f[2]}

Implementation notes

builtin_symbol (src/core.c) converts a string to a symbol. It validates the name with symbol_name_is_valid (each backtick-delimited context segment must start with a letter or $ and continue with alphanumerics/$), runs it through context_resolve_name to apply the current context, and returns an EXPR_SYMBOL. Invalid names emit a Symbol::symname message and return NULL.

  • Protected.
  • Every expression's Head matches Symbol for symbols; x_Symbol patterns therefore match any symbol.
  • The string must satisfy the standard symbol-name syntax: each segment (separated by backticks) starts with a letter or $, followed by letters, digits, or $.
  • A leading backtick (Symbol["\x"]) makes the name relative to the current$Context. An embedded backtick (Symbol["a`x"]) is treated as an absolutely-qualified name. A bare name is resolved through the standard$Context/$ContextPath` rules.
  • Invalid names emit Symbol::symname to stderr and leave the call unevaluated; non-string arguments also leave the call unevaluated.

Attributes: Protected.

References

See also: Head, $Context, $ContextPath

Notes & additional examples

Notes

Symbol["name"] returns the symbol with the given name, creating it if it does not yet exist. The name must satisfy the standard symbol-name syntax: each backtick-delimited context segment starts with a letter or $ and continues with letters, digits, or $. A leading backtick makes the name relative to the current $Context, an embedded backtick gives an absolutely-qualified name, and a bare name is resolved through $Context / $ContextPath.

Because every symbol's Head is Symbol, an x_Symbol pattern matches any symbol and nothing else. An invalid name emits Symbol::symname and leaves the call unevaluated; a non-string argument does likewise.