String literal at:put: question

Bob Arning arning at charm.net
Mon Dec 25 17:26:17 UTC 2000


On Mon, 25 Dec 2000 11:46:11 -0500 "Andrew C. Greenberg" <werdna at mucow.com> wrote:
>>That has been the Smalltalk behavior since the dark ages... I presume
>>the intention is to make it semantically similar to assignment
>>expressions, whose value is equal to the assigned value. To achieve your
>>intention, you should evalutae
>>     'abcdef'
>>         at: 1 put: $x;
>>         yourself
>>which returns the receiver of the first message expression.
>
>Right.  Also, if it were otherwise, there would be no way to operate 
>on the expression for the put: operand.  #yourself gives access to 
>the receiver, while the returned value gives access to the assigned 
>value.
>
>Why you would want this, rather than to assign the expression to a 
>temporary is beyond me, but hey, syntax like this is what makes 
>obfuscated code contests fun to enter!

One reason is that it doesn't require a temporary, which helps keep the clutter down. Look, for instance at the implementation of #at:ifAbsentPut:

at: key ifAbsentPut: aBlock 
	"Return the value at the given key. 
	If key is not included in the receiver store the result 
	of evaluating aBlock as new value."

	^ self at: key ifAbsent: [self at: key put: aBlock value]

which, if #at:put: returned the receiver rather than the second argument, would become

at: key ifAbsentPut: aBlock 
	"Return the value at the given key. 
	If key is not included in the receiver store the result 
	of evaluating aBlock as new value."

	| blockResult |

	^ self at: key ifAbsent: [self at: key put: (blockResult _ aBlock value). blockResult]

I know which one I perfer. ;-)

Cheers,
Bob





More information about the Squeak-dev mailing list