Polymorphism without protocol dilution

David N. Smith dnsmith at watson.ibm.com
Wed Sep 2 14:17:08 UTC 1998


>At 9:01 -0400 9/2/98, Jarvis, Robert P. 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
>
>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.

The code:

   d at: k ifAbsentPut: aBlock

has the advantage of not evaluating aBlock unless needed. The evaluation
might involve some computation which you'd want to avoid unless necessary.


>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 :-).

Me either. I think he must have something else in mind.

Dave
_______________________________
David N. Smith
IBM T J Watson Research Center
Hawthorne, NY
_______________________________
Any opinions or recommendations
herein are those of the author
and not of his employer.





More information about the Squeak-dev mailing list