4 ms·
My favorite individual feature of this language, reading through the spec, is the explicit tail-call statement: sp2_help( bits32 n, bits32 s, bits32 p ) {
by shadowofneptune 5y ago
My favorite individual feature of this language, reading through the spec, is the explicit tail-call statement:
sp2_help( bits32 n, bits32 s, bits32 p ) {
if n==1 {
return( s, p );
} else {
jump sp2_help( n-1, s+n, p*n );
}
}
Clang has a similar thing now, but it's an attribute not a language keyword. There's two things I like about it:
First, it communicates intent better. For someone not familiar with the principle of a tail call or a sibling call, thinking of it as a goto is easier, and is what the compiler is actually doing underneath to prevent a stack overflow.
Second, it allows for the compiler to see if the tail call is well-formed right then and there. It would then flag an invalid one as an error instead of not optimizing the tail call. This prevents any surprises at runtime.
For a higher-level language than C--, it'd be fun to call this explicit tail-call statement goto, and have no other form of goto statement. It's just as powerful in expression as the classic goto while avoiding its pitfalls.
- rabbits77 5y agoPerl has a similar syntax, it is quite nice sub my_funct{ my($x, $y) = @_; #… goto &my_funct($x, $y); } In Perl this is convenient because effectively with just adding the goto keyword you reduce all the memory use and risk of blowing your stack (recursion is otherwise super inefficient in Perl).
- shadowofneptune 5y agoIt's fascinating to find an idea like that in Perl. Python's developers are hostile to tail-call optimization due to reasons like it complicating debugging.