This is outputting false
instead of true
. Why is this?
class Main {
function void main() {
var String foo;
let foo = "bar";
if (foo = "bar") {
do Output.printString("true");
}
else {
do Output.printString("false");
}
return;
}
}
I suspect it's because foo
and "bar"
are each objects, and the =
tests whether the starting address of each object is the same (note that in Jack, equality is tested for with a single equals, rather than a double or triple equals). I haven't gotten to chapters 10/11 yet, which is when I'll discover whether or not this hypothesis is true.
Sorry for the late answer, but here it comes. Your program will dynamically create a string on the heap for every occurrence of "bar" and your code will compare two distinct addresses on the heap. The comparison will be false.
How do I know that? I have just finished writing the compiler...