Administrator
|
No, it's not a defect. It's part of the language specification, though not specifically called out.
You actually see this same behavior in many languages in which two objects are equal if and only if they refer to the same object. If you want to compare the equality of the contents of two different objects, then you have to use a function specifically intended for that purpose. In C, for strings, that function is strcmp(). Some languages try to make the language semantics more closely match human intuition and do a pretty good job for simple structures like strings, but even these you quickly get into the same issue for more advanced data structures.
What IS a defect in the language design is that Jack allows you to use string literals in places that prevent you from cleaning them up, leading to a memory leak.
Consider the code you gave. If you don't free up foo before exiting the function, the memory allocated to "bar" is lost. In this case, that's on the programmer because they allocated the memory and didn't deallocate it. But if you call a function that takes a string argument Jack allows you to use a string literal, but the called function generally doesn't (and shouldn't) deallocate the memory and once the function returns there's no way to deallocate it.
Real programming languages face the same issue and handle it in different ways. For languages that have automatic garbage collection, it is just incorporated into the normal garbage collection process. For languages that don't, a common way of dealing with it is for the compiler to incorporate all of the literal strings in the code into the code itself (they've got to be there one way or the other) and not put them into data memory at all. This can have some subtle effects and can also be a potential security vulnerability.
|