Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is dollar sign $ used for internally?

What is the symbol $ used for internally?

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.

like image 497
Mr.Wizard Avatar asked Oct 22 '25 05:10

Mr.Wizard


2 Answers

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."

like image 63
librik Avatar answered Oct 25 '25 07:10

librik


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.

like image 36
Sasha Avatar answered Oct 25 '25 08:10

Sasha



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!