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.
iOS is over 50% of the mobile browser share in the US and desktop Safari is 11-12% (roughly 1 in 9) and both implement proper tail calls and have for around 6 years now. Despite this, the world has not collapsed and people aren't constantly complaining that websites are constantly breaking.
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?