Earlier quoted context omitted.
Optimizing calls using the same syntax as regular ones risk introducing stack overflow without noticing. 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 th…
I don't quite get the argument. Isn't this similar to e.g requiring the `z` parameter to be annotated with something that ensures that it is a small number? ("What is the downside of having special syntax for that?") My undrstanding is that a tail call is a tail call , and the variant with +1 is not that, thus being a candidate to be looked at. Perhaps also IDEs can help here if it's difficult to spot?
You mean something like "unsigned short int" :) ? Another example would be "const". What's the point - either the variable is const or it isn't. Programmer can just remove the mutation.
> My undrstanding is that a tail call is a tail call, and the variant with +1 is not that, thus being a candidate to be looked at.
Sure, but don't you agree it's an easy mistake to make? Especially when the recursion goes through several different functions. Additionally - you reading the code might not realize it's important for this function to continue to be tail-recursive.
> Perhaps also IDEs can help here if it's difficult to spot?
Or perhaps a compiler can do it?