Any reason for assigning block parameter in inject:into:

tim Rowledge tim at rowledge.org
Sun Apr 29 16:03:43 UTC 2007


On 29-Apr-07, at 2:36 AM, Lukas Renggli wrote:

>> There's absolutely no way that assigning to a block arg should be
>> permitted. Blocks arg should be treated the same as method parameters
>> by the compiler.
>
> A question: Why shouldn't it be allowed to assign to method and block
> arguments, if the underlying implementation allows to do so?

Method parameters cannot meaningfully be assigned to because you  
would not be assigning to what you might think you are assigning  to.  
A method parameter is a slot in the method context, associated with a  
name for the programmer's convenience. It is*not*  the named object  
that you pass with the message send.
For example
Foo > bar
|obj1|
obj1 := 'fred'.
self fibble: obj1.
^obj1

Foo>fibble: myParam
myParam := 'jim'

Ok, so what happens? What is the result of sending #bar (this assumes  
that the compiler has a bug and lets you compile such code)?

It had better be 'fred'! You see, in the fibble: method 'myParam' is  
imply a named slot where the OOP of the parameter is place during the  
message send. It is *not* a pointer to the variable obj1. We don't  
have pointers in Smaltalk; people do seem to forget that  
occasionally. So, in #bar
the obj1 temporary variable slot is filled with the OOP for an  
instance of a String with the characters for 'fred',
the OOP stored at obj1 is pushed onto the stack,
the message #fibble: is sent and the OOP on the stack is copied to  
the new method context,
the new method context starts executing
the named slot 'myParam' gets filled with the OOP for a new String  
instance with the characters 'jim'.
the #fibble method context returns
the #bar method context returns with the OOP from the named slot 'obj1'.

That returned object is 'fred'.  If the compiler did allow assigning  
to method parameters it would do nothing but cause confusion and,  
quite possibly, the end of civilisation.

A similar issue pertains for block arguments but we have the added  
complication that even after ELEVEN YEARS we still don't have proper  
closures in the default system and so the compiler can't really get  
it's act together to work out that block args (and indeed block  
temps) need to be treated appropriately.

A long time ago when CS was trying to be an actual science instead of  
a label disguising half-hearted attempts to produce semi-trained code- 
monkeys for java sweatshops there used to be discussions about 'pass  
by value' and pass by reference'. There is an old paper from '85  
(Smalltalk-80 Newsletter #5) by Kim McCall evaluating Smalltalk in  
this context. Finding a copy, reading it and understanding it is left  
as an exercise to the student.

tim
--
tim Rowledge; tim at rowledge.org; http://www.rowledge.org/tim
"Bo***x" said Pooh when Piglet kneed him in the groin.





More information about the Squeak-dev mailing list