What is dollar sign $ used for internally?
What is the symbol $
used for in开发者_Go百科ternally?
I do not mean the compound forms x$388
or $5
etc., just the $
by itself.
I am wondering if this is a valid object to use in notation, or what I will break if I do.
It is unwise to have user variables that end in an odd number of $ characters (not counting the first character). x$, y$$$, and $$ are all poor choices for variable names.
This is because appending an odd number of $ to an identifier is a technique called "lexical renaming," which the Mathematica kernel uses to avoid conflicts in variable names when higher-order functions return functions that use the same variable names as their parents. This technique is used in a variety of scoping constructs, including Function, Module, With, and Rule; here is an example with Function:
In[1]:= f = Function[{x, y}, Function[{x}, x+y]]
Out[1]= Function[{x, y}, Function[{x}, x + y]]
In[2]:= f[2,3]
Out[2]= Function[{x$}, x$ + 3]
In[3]:= ?*`x$
Global`x$
Attributes[x$] = {Temporary}
In short, appending $ characters is a system-internal renaming mechanism, and identifiers of this form are recognized by Mathematica as "lexically renamed" versions of the $-less forms, with Temporary attribute. It is not recommended to use variables of this form in your own code.
Mathematica is a term-rewriting language that can behave like a lexically scoped functional language by use of internal rewriting mechanisms such as "lexical renaming."
In version 7, symbol System`$
used to be already created in a fresh kernel, but not used for anything as far as I know. In version 8, symbol $
is not pre-created:
In[1]:= Context["$"]
During evaluation of In[1]:= Context::notfound: Symbol $ not found. >>
Out[1]= Context["$"]
I would agree with Szabolcs that code using $
in System
context might break in future versions, as well as any other code that modifies System symbols.
精彩评论