This is just an random idea that popped into my head. Tell me if I’m crazy.
Is there enough distinction between literals in code and values generated at runtime? In other words, what should be the difference between this:
x = "Hello, World!"
and this (syntax made up):
x = new String("Hello, World!")
In general, I understand literal values in code to be immutable, or at least it’s undefined what happens if you modify them. So should two identical literals be pointers to the same block of memory? Does this C code:
char x[] = "Hello!";
char y[] = "Hello!";
if (x == y) printf("True");
else printf("False");
print True? (I tried it, and the answer is no, it doesn’t.)
This gets more confusing when you can have object literals as in JavaScript:
myFriend = {
name : "Bob",
age : 32,
friends : ["Jack", "Jill", "Joe"],
talk : function(){alert("Hi, I'm "+myFriend.name)}
};
Is myFriend
mutable? As it turns out, in JavaScript, the answer is yes. For an interpreted language embedded in web pages that makes sense. But what about a compiled language — is efficiency lost when values could be compiled in but aren’t because the compiler doesn’t know the value never changes? I suppose that’s the reason for all those annoying const
declarations in C++, but is that a complete solution?
So my question is this: would it be worthwhile to have specific syntax for compile-time contants? C has preprocessor constants, but they’re text-based and don’t carry type information.