Concerning #ifAbsentPut:

Bob Arning arning at charm.net
Wed Sep 2 13:58:37 UTC 1998


Hi Bob,

I think it may prove useful to create a summary of the different proposals so we can compare them side-by-side. I'll try to put this together shortly.

On Wed, 2 Sep 1998 09:01:06 -0400 "Jarvis, Robert P." <Jarvisb at timken.com> wrote: 
>There's something here I'm probably just not getting, but I don't
>see the value (no pun intended) of having Dictionary>>at:ifAbsentPut:
>work differently for blocks vs. other objects.  Bob Arning suggests
>that we should have 
>
>d at: k ifAbsentPut: 0	"inserting (and returning) 0 if k is not a key in
>d"
>d at: k ifAbsentPut: aBlock		"inserting (and ^) the result of evaluating
><aBlock>"
>d at: k ifAbsentPut: [aBlock]		"inserting (and ^) aBlock"
>
>Why not just have Dictionary>>at:ifAbsentPut: add and return the
>'ifAbsentPut'
>object?  If you want to shove a non-Block object in, fine.  If you want
>to put the
>result of evaluating a Block into the dictionary then you'd write
>
>	d at: k ifAbsentPut: aBlock value
>

What I don't like about this is that you would always evaluate <aBlock> whether you needed the result or not.


>If you want to put a Block into a dictionary, then write
>
>	d at: k ifAbsentPut: aBlock
>
>I don't understand why Dictionary>>at:ifAbsentPut: should be
>special-cased
>for Blocks, as it doesn't seem necessary to me.
>

Depends on how you look at it: all the other #ifXxxx: protocols evaluate the argument if and only if certain conditions are met. From this perspective, the only "special case" is to allow objects other than blocks (such as integers, strings, etc) to understand #value. Some have complained that this dilutes the #value protocol, but I am comfortable with it.

>One other thing - I don't understand why the last two of Bob's examples
>are different.  It seems to me that what's being said is that
>
>	aBlock := [ "do something" ]
>	d at: k ifAbsentPut: aBlock
>
>and
>
>	d at: k ifAbsentPut: [ "do something" ]
>
>are somehow different.  I don't see how the method would know
>the difference.  (I suspect that ten seconds after sending this
>message I'll suddenly figure this out and be very embarassed :-).

If, by the last two, you mean:

>d at: k ifAbsentPut: aBlock		"inserting (and ^) the result of evaluating
><aBlock>"
>d at: k ifAbsentPut: [aBlock]		"inserting (and ^) aBlock"

then, I might rewrite them as

d at: k ifAbsentPut: [3+4]		"inserts a SmallInteger 7"
d at: k ifAbsentPut: [[3+4]]		"inserts a block which, when later #value'd, will answer 7"

which would be equivalent to:

aBlock _ [3+4].
d at: k ifAbsentPut: aBlock.		"inserts a SmallInteger 7"
d at: k ifAbsentPut: [aBlock]	.	"inserts a block which, when later #value'd, will answer 7"

Cheers,
Bob





More information about the Squeak-dev mailing list