blocksmalltalkpharo

Why does this code result in a "Cannot store into ->" error message?


Executing this code in Pharo results in a Cannot store into -> error message?

[ :x |
x := x + 33.
x + 2] value: 5 

Is this a logical consequence of Smalltalk's syntax, or is it a consequence of the Smalltalk's semantics and their execution?


Solution

  • The variable x is a parameter to the block. What you see is a kind of safety measure to avoid unexpected behavior of the program to the progammer. It is something dependent on the dialect (some prohibit assignment to arguments, others warn, others do nothing).

    Assignment to an argument is seen as a code smell, and that's the reason why it is prohibited.

    From a VM/compiler perspective, it isn't hard to support assignment to arguments, your code is perfectly valid, and actually it takes some extra coding effort to prohibit it.