Polymorphism without protocol dilution

Travis Griggs tgriggs at keyww.com
Mon Aug 31 18:12:33 UTC 1998



David N. Smith wrote:

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

Why don't we just add types to Smalltalk! Seriously people, that's what all of
these messages are starting to look like. I can just see some of my methods in
the future:

at: aKey ifAbsentPutValueOfOneArgBlockThatWontReturnNil: aBlock

Not that I necessarily have a better solution :). But it worries me when I see
messages whose keyword parts are basically types. A little of this is OK, but I
think it should be used sparingly.

--
Travis Griggs
Key Technology
tgriggs at keyww.com
Smalltalk - 100% Pure Objects, Always had 'em, Always will!





More information about the Squeak-dev mailing list