Any reason for assigning block parameter in inject:into:

Keith Hodges keith_hodges at yahoo.co.uk
Sun Apr 29 16:17:05 UTC 2007


tim Rowledge wrote:
>
> 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.
I am not confused, and that is saying something!

Personally I think its a great idea, and what you described is exactly 
what I would expect from smalltalk, knowing that it doesnt do pointers.

What you are saying is that the named parameter that comes into the 
method which looks like a variable, and you use it like a variable is 
actually a constant within the context of that method. Its actually 
counter intuitive.

Keith



More information about the Squeak-dev mailing list