In PHP and Python one can iterate over the local variables and, if there is only once choice where the value matches, you could say that you know what the variable’s name is, but this does not always work. Machine code does not have variable names. C compiles to assembly and does not have any native reflection capabilities, so it would not know it’s name. (Edit: per Anton’s answer the pre-processor can know the variable’s name).
Do there exist programming languages where a variable would know it’s name?
It gets tricky if you do something like b = a
and b
does not become a copy of a
but a reference to the same place.
EDIT: Why in the world would you want this? I can think of one example: error checking that can survive automatic refactoring. Consider this C# snippet:
private void CheckEnumStr(string paramName, string paramValue)
{
if (paramName != "pony" && paramName != "horse")
{
string exceptionMessage = String.Format(
"Unexpected value '{0}' of the parameter named '{1}'.",
paramValue,
paramName);
throw new ArgumentException(exceptionMessage);
}
}
...
CheckEnumStr("a", a); // Var 'a' does not know its name - this will not survive naive auto-refactoring
There are other libraries provided by Microsoft and others that allow to check for errors (sorry the names have escaped me). I have seen one library which with the help of closures/lambdas can accomplish error checking that can survive refactoring, but it does not feel idiomatic. This would be one reason why I might want a language where a variable knows its name.
7
It depends on what you mean by name. If we rephrase the question as “can we extract, from any variable, a unique identifier for that variable” then in C the answer is a definite yes: the address-of operator will give us that. In code:
int x;
// &x now uniquely identifies x (while it is valid).
If we rephrase it as “given an identifier, can we apply an operation to it to get a string containing it”, then the answer in C is once again yes: we can make a macro which, applied to the identifier, will stringify it. Once more in code:
#define str(s) str2(s)
#define str2(s) #s
int x;
// str(x) will now be "x"
If we look in the context and C++, and ask “given an lvalue reference, can we get a textual representation with which this reference was first defined”, the answer is no. In fact, there need not be such a variable: we may have a reference to const which was initialised with an rvalue.
It would definitely be possible to create a language similar to C++, but where this information can be found. Thus, theoretically, such a language exists. I am unaware of any that support this in practice; the question as phrased above is mostly meaningful in a language where things are indeed declared, and such languages tend to shy away from the runtime overhead that this would require.
Reaction to edit: this depends on how the auto-refactoring tool works. With the macro solution, the call would be CheckEnumStr(STR(a), a);
. The tool could decide to treat the macro invocation as a function call and rename the a
, or expand the macro and not modify the "a"
.
6