Earlier quoted context omitted.
Goto can be anything - a function call, a loop, an if clause, exception handler. That's the whole reason we created structured programming - so we don't have to guess what it is THIS time. Tail call optimization is nice, but it has its problems. What's the downside to requiring special syntax for it? It frees the programmer reading the code from determining if it's a tail call every time.
Right, but optimizing tail calls won't lose that structure. But sure, just using goto is a slippery slope to confusion.
function rec(x, y, z) {
if (something(z)) {
return rec(x, y, z-1);
}
return 1;
}
Now you need to add 1 to the result. function rec(x, y, z) {
if (something(z)) {
return rec(x, y, z-1) + 1;
}
return 1;
}
Ups, now it fails with stack overflow for big z values. Hope you have good unit tests to catch this.To avoid this problem whenever you modify a possibly recursive function in a language with transparent TCO - you have to look through the code to see if it's using TCO. This is wasted time. And it can be non-trivial if you have recursive functions calling each other and other funny stuff.
If instead the language required special syntax for TCO:
function rec(x, y, z) {
if (something(z)) {
return TAIL_CALL rec(x, y, z-1) + 1;
}
return 1;
}
This would be a compilation error cause it's not a tail call contrary to the declaration.What is the downside to requiring special syntax?