Polymorphism without protocol dilution

David N. Smith dnsmith at watson.ibm.com
Fri Aug 28 17:08:09 UTC 1998


At 0:39 -0400 8/26/98, Maurice Rabb wrote:
>I would appreciate any thought on the following:
>
>I though I would add the common method #at:ifAbsentPut: to Dictionary.
>
>As the convention varies between dialects, I though it would be useful for
>the 'ifAbsentPut: parameter' to accept either a value, or a block to be
>evaluated.
>
>My first iteration was:
>
>at: key ifAbsentPut: valueObject
>    ^self at: keyObject ifAbsent: [self at: key put: valueObject value]
>
>For this to work would require that I add Object>>#value which I remember
>is a no-no, because it dilutes the BlockContext protocol as Object>>#do:
>diluted the Collection protocol.  We replaced Object>>#do: with #in:, so
>with what do we replace Object>>#value?


I'd like to make a third kind of suggestion for your list. Don't mix blocks
and other values. Make the protocol different. For example:

   at: key ifAbsentPut: anObject
      self at: key ifAbsent: [ self at: key put: anObject ]


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


Another effect of this suggestion is that it still allows putting blocks
into dictionaries as values, while suggestions 1 and 2 do not.


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