pbpublist at gmail.com
Wed Dec 30 22:58:48 CST 2015
On Thu, 2015-12-31 at 14:23 +1100, Jonathan Kelly via Cuis-dev wrote:
> Hi All,
> I'm a relative newbie to Smalltalk, having dabbled in the past, but
> decided to get into it proper for some projects I have in mind.
> So, I've written the code for BTree as a learning exercise and was
> thinking about the Interface and thought Dictionary would be a
> protocol to follow. Heavens - they think someone can understand all
> at once? Funny they never say how much effort that would take, or
> your IQ would have to be. I guess that's just me being overwhelmed.
Heh, yes... it can take some getting used to being exposed to every
class and method w/ source and little guidance vs. the more
conventional languages/libraries where they pretty much say 'do it this
> Anyway, I discovered what I would say was a nightmare waiting to
> .. for someone, but it would seem not, as I'm assuming that code for
> Dictionary>>add: has been around for along time. Interested to hear
> experienced smalltalk-er views.
> > aDict assocA assocB |
> aDict := Dictionary new.
> assocA := 'A' -> 123.
> assocA printString. "'A' -> 123"
> aDict add: assocA.
> assocB := 'A' -> 456.
> aDict add: assocB.
> assocA printString. "'A' -> 456"
I would highly recommend not adding associations directly as that's an
exposed implementation detail that may change (and has in the past... I
seem to recall a method to add associations that was used in several
places being removed) Probably safer to do something along the lines
of: aDict at: 'A' put: 123.
Beyond that, are you wondering why your 123 was replaced by 456? If
so, it's because the test used by Dictionary is equality (i.e. 'A'='A'
even if they are two different instances of the string 'A') rather than
identity (i.e. testing for the same object) There are very few places
in Smalltalk where you will see object identity used that way and it is
generally discouraged unless absolutely necessary. If what you really,
really wanted to be used is an identity test there is always... wait
for it... IdentityDictionary :-)
More information about the Cuis-dev