[squeak-dev] The Inbox: Collections-ct.860.mcz

Levente Uzonyi leves at caesar.elte.hu
Sun Nov 17 22:45:21 UTC 2019


On Sun, 17 Nov 2019, Thiede, Christoph wrote:

> 
> Okay, I was thinking that #add: would raise an error if the key is already present. But it doesn't and replace the value.

In my opionion, Dictionary >> #add: is a remnant of the old days when
bindings (class, global and pool) were always assotiations. It's goal was
to let multiple dictionaries share the same binding.
But, with the introduction of Binding & subclasses, this turned out to be
a harder problem, as their classes need to be changed accordingly. That's 
something that isn't always done properly. E.g. if you have a Global in 
Undeclared, and declare it as a class variable, the binding's class will 
stay Global, however if you declare a class variable not in Undeclared, 
its binding will be an Association. Also, from wherever that Global was 
held a reference to, the reference is still there exposing the class 
variable.

Using #add: in #fromKeys:values: causes problems with dictionaries having
their own #associationClass returning anything other than Association.

> 
> (Which I find a bit confusing because if the key is already present, it deletes the old value silently.)
> 
> The only method that raises an error if a key is already present seems to be Dictionary>>#newFrom:.
> 
> 
> Maybe this commit should be edited (diff-diff):
> 
> + ----- Method: Dictionary class>>fromKeys:values: (in category 'instance creation') -----
> + fromKeys: keys values: values
> +
> +       | dictionary |
> +       self assert: [keys size = values size].
> +       dictionary := self new: keys size.
> +       keys with: values do: [:key :value |
> 
> - +               dictionary add: key -> value].
> 
> + +               dictionary
> 
> + +  at: key
> + +  ifPresent: [:existing | self error: ('Duplicate key "{1}"' format: {key})]
> + +  ifAbsentPut: value].

You can't avoid the block around value, the argument of ifAbsentPut:. If
you did so, and value were a block, it would be evaluated, and the value
returned by that block would be stored in the dictionary instead of value 
itself.


Levente

> 
> +       ^ dictionary!
> 
> _________________________________________________________________________________________________________________________________________________________________________________________________________________________________
> Von: Squeak-dev <squeak-dev-bounces at lists.squeakfoundation.org> im Auftrag von Nicolas Cellier <nicolas.cellier.aka.nice at gmail.com>
> Gesendet: Sonntag, 17. November 2019 18:37:07
> An: The general-purpose Squeak developers list
> Betreff: Re: [squeak-dev] The Inbox: Collections-ct.860.mcz  
> Hi Christoph,
> I presume that Jakob is just suggesting using (at: key put: value) rather than (add: key -> value).
> 
> Le dim. 17 nov. 2019 à 17:36, Thiede, Christoph <Christoph.Thiede at student.hpi.uni-potsdam.de> a écrit :
>
>       Thank you for the review! This was actually intended, or what would expect the following to do?
> 
>
>       Dictionary fromKeys: 'wasda' values: '84265'
> 
> _________________________________________________________________________________________________________________________________________________________________________________________________________________________________
> Von: Squeak-dev <squeak-dev-bounces at lists.squeakfoundation.org> im Auftrag von Jakob Reschke <forums.jakob at resfarm.de>
> Gesendet: Sonntag, 17. November 2019 17:24:25
> An: squeak-dev at lists.squeakfoundation.org
> Betreff: Re: [squeak-dev] The Inbox: Collections-ct.860.mcz  
> Is there a strong indication to use add: anAssociation over at:put: here? The latter is more idiomatic and does not use the implementation detail that a dictionary is made up of associations. 
> 
> 
> <commits at source.squeak.org> schrieb am Fr., 15. Nov. 2019, 13:59:
>       A new version of Collections was added to project The Inbox:
>       http://source.squeak.org/inbox/Collections-ct.860.mcz
>
>       ==================== Summary ====================
>
>       Name: Collections-ct.860
>       Author: ct
>       Time: 15 November 2019, 1:58:56.418005 pm
>       UUID: f6364e7f-08c8-694a-9060-21bc0a0c28d4
>       Ancestors: Collections-pre.857
>
>       Add convenience constructor for Dictionary
>
>       Dictionary fromKeys: 'wasd' values: '8426'
>
>       =============== Diff against Collections-pre.857 ===============
>
>       Item was added:
>       + ----- Method: Dictionary class>>fromKeys:values: (in category 'instance creation') -----
>       + fromKeys: keys values: values
>       +
>       +       | dictionary |
>       +       self assert: [keys size = values size].
>       +       dictionary := self new: keys size.
>       +       keys with: values do: [:key :value |
>       +               dictionary add: key -> value].
>       +       ^ dictionary!
> 
> 
> 
> 
>


More information about the Squeak-dev mailing list