[squeak-dev] The Inbox: Collections-mt.932.mcz

Thiede, Christoph Christoph.Thiede at student.hpi.uni-potsdam.de
Fri Mar 26 18:50:29 UTC 2021


Hi Marcel,


I'm a bit afraid of creating more subclasses of Collection or Dictionary for two reasons:


1. Different subclasses are incompatible. If you wanted a special dictionary that has weak keys and holds collections, you would need to create another subclass and duplicate some of the logic.

2. Such a design change could introduce ambivalence and doubts about what subclass to choose, and if you choose the wrong class, you will need to do additional conversions. For example, should Collection >> #groupBy: return CollectionsDictionary instances with your proposal? If there is any other legacy code that does not use CollectionsDictionary, you will need to check or fix its type to avoid DNUs when further logic might be added to CollectionsDictionary later. Sometimes I have the feeling that the approach of creating too many collection/dictionary subclasses could violate the idea of polymorphy because they effectively remove genericity from their subclasses and refuse a lot of bequest (e.g., the math functions protocol from Collection).


That being said, I'm not completely against the idea, but these are some common concerns I have on collection subclassing in general. I would like to avoid ending up with a convoluted design such as .NET has, which contains next to the generic List<T>: StringList, StringCollection, StringDictionary, ListCollection, IntCollection, etc. ... :-)


Hypothetically, an alternative approach would be to add a new protocol to an existing superclass, for example 'list enumerating' on Dictionary, which would provide for compatibility with other data structures. For example, existing items could even be wrapped into a list when doing something like:


myDict at: #fruits put: #apple.

(myDict makeListAt: #fruits) addAll: #(banana cherry date).

(myDict makeListAt: #sweets) add: 'eclair; add: 'froyo'; add: 'gingerbread'.


See Collection >> #associationsDo: for another example. What do you think about it? :-)


However, these are only my 2 cents without a clear opinion. Looking forward to your arguments! :-)


Best,

Christoph


________________________________
Von: Squeak-dev <squeak-dev-bounces at lists.squeakfoundation.org> im Auftrag von Taeumel, Marcel
Gesendet: Freitag, 26. März 2021 19:08 Uhr
An: squeak-dev
Betreff: Re: [squeak-dev] The Inbox: Collections-mt.932.mcz

Hi all!

What do you think? Does it makes sense to pursue this idea of "dictionaries with collections as values" and enrich it with tests and documentation?

Best,
Marcel

Am 26.03.2021 19:05:09 schrieb commits at source.squeak.org <commits at source.squeak.org>:

A new version of Collections was added to project The Inbox:
http://source.squeak.org/inbox/Collections-mt.932.mcz

==================== Summary ====================

Name: Collections-mt.932
Author: mt
Time: 26 March 2021, 7:04:57.368373 pm
UUID: b6c0c2c9-0a4f-684c-87a7-069813edd154
Ancestors: Collections-dtl.931

Sketch of a dictionary that has collections as values.

dict := CollectionsDictionary new.
(dict at: #fruits) addAll: #(apple apple peach).
(dict at: #sweets) add: #nougat.
dict removeKey: #sweets.
dict explore.

=============== Diff against Collections-dtl.931 ===============

Item was added:
+ Dictionary subclass: #CollectionsDictionary
+ instanceVariableNames: 'collectionClass'
+ classVariableNames: ''
+ poolDictionaries: ''
+ category: 'Collections-Unordered'!

Item was added:
+ ----- Method: CollectionsDictionary class>>defaultCollectionClass (in category 'defaults') -----
+ defaultCollectionClass
+
+ ^ OrderedCollection!

Item was added:
+ ----- Method: CollectionsDictionary class>>newOnBags (in category 'instance creation') -----
+ newOnBags
+
+ ^ self new
+ setCollectionClass: Bag;
+ yourself!

Item was added:
+ ----- Method: CollectionsDictionary class>>newOnOrderedCollections (in category 'instance creation') -----
+ newOnOrderedCollections
+
+ ^ self new
+ setCollectionClass: OrderedCollection;
+ yourself!

Item was added:
+ ----- Method: CollectionsDictionary class>>newOnSets (in category 'instance creation') -----
+ newOnSets
+
+ ^ self new
+ setCollectionClass: Set;
+ yourself!

Item was added:
+ ----- Method: CollectionsDictionary>>at: (in category 'accessing') -----
+ at: key
+
+ ^ self at: key ifAbsent: [super at: key put: collectionClass new]!

Item was added:
+ ----- Method: CollectionsDictionary>>at:put: (in category 'accessing') -----
+ at: key put: value
+
+ self shouldNotImplement.!

Item was added:
+ ----- Method: CollectionsDictionary>>collectionClass (in category 'accessing') -----
+ collectionClass
+
+ ^ collectionClass!

Item was added:
+ ----- Method: CollectionsDictionary>>initialize: (in category 'initialization') -----
+ initialize: n
+
+ super initialize: n.
+ self setCollectionClass: self class defaultCollectionClass.!

Item was added:
+ ----- Method: CollectionsDictionary>>setCollectionClass: (in category 'initialization') -----
+ setCollectionClass: aClass
+
+ collectionClass := aClass.!


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.squeakfoundation.org/pipermail/squeak-dev/attachments/20210326/c2d6a996/attachment-0001.html>


More information about the Squeak-dev mailing list