On Sun, Dec 30, 2012 at 7:40 AM, Bert Freudenberg <b...@freudenbergs.de> wrote:
> On 2012-12-27, at 01:32, Sebastian Nozzi <sebno...@gmail.com> wrote:
>
>> Why do ST methods return "self" if nothing is explicitly returned?
>
>
> One very simple reason has not been stated yet: In the Virtual Machine, 
> returning self is simpler and more efficient than returning any other object.
>
> Smalltalk byte codes implement a stack machine. That means arguments are 
> passed by pushing them onto a stack, rather than putting them into registers. 
> In addition to the arguments as listed in the method signature, a hidden 
> argument is always passed, which is the receiver of the message. So even for 
> unary methods (those without arguments) the receiver is pushed onto the 
> stack, then the method is executed, and the receiver value on the stack is 
> how the method knows "self". By returning "self" if no explicit return 
> statement is given, the VM can just leave the "self" oop on the stack, which 
> saves at least one memory store operation. So from an efficiency and 
> simplicity standpoint, returning "self" is the most elegant thing to do.

I thought of it (when I wrote the reply) but isn't this really the
argument for returning self instead of nil for example?  Any message
send pops all arguments including the receiver and pushes the return
value so "self" is not on the stack.  Typical byte code sequence for a
method that returns self is popping the last result and ends the
sequence with "returnSelf"; so it should be equally efficient if such
a method endsWIth "returnNil", if such bytecode exists?

--
-- Yoshiki
_______________________________________________
Beginners mailing list
Beginners@lists.squeakfoundation.org
http://lists.squeakfoundation.org/mailman/listinfo/beginners

Reply via email to