Do there exist programming languages where a variable can truly know its own name?

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

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật