Is Squeak tail recursive?

Jesse Welton jwelton at pacific.mps.ohio-state.edu
Fri Jan 29 17:23:19 UTC 1999


Lex Spoon writes:
> 
> amoroso at mclink.it (Paolo Amoroso) wrote:
> > Does the Squeak compiler generate properly tail recursive code?
> > 
> > 
> 
> I don't think so.  As a defense, optimizations are generally more
> complicated in Smalltalk than in non-OO languages, because of the late
> binding.
> 
> In this case, suppose you have a class Bar, with a method as follows:
> 
> 	func: x
> 		(x cos - x) abs < 0.0001 ifTrue: [ ^x ].
> 		^self func: x cos
> 
> A nice, tail recursive function, right?  Well, suppose we then add a
> subclass Bar, which defines a new version of func: as follows:
> 
> 	func: x
> 		Trascript show: x printString.
> 		^super func: x
> 
> 
> Well, now func: isn't always tail recursive--it depends on whether
> "self" is a Foo or a Bar!

This is true enough in the restricted sense of direct recursion of
func: on itself.  However, I don't see why general tail call
optimization would not be possible, or would not result in proper
mutual tail recursion in this case.

There are a couple of places where I can imagine it might be somewhat
problematic, but they don't have to do with OO at all.  The things
that come to mind are debugging issues, like not having the complete
call stack available for inspection.  I don't think this would be a
terrible loss in most cases, but it seems to me that alot of
Smalltalkers are accustomed to having a great deal of context
information available in the debugger.

-Jesse Welton





More information about the Squeak-dev mailing list