[OT] Finding longest documents
Andrei Alexandrescu
SeeWebsiteForEmail at erdani.org
Wed Oct 15 07:17:57 PDT 2008
Benji Smith wrote:
> Andrei Alexandrescu wrote:
>> Benji Smith wrote:
>>> Andrei Alexandrescu wrote:
>>>> Christopher Wright wrote:
>>>>> Containers as value types is such an idiotic idea for precisely
>>>>> this reason.
>>>>
>>>> I'd say this statement reflects a lack of understanding of value
>>>> types, rather than value containers are an idiotic idea.
>>>
>>> I'll bite.
>>>
>>> Why is it a good idea that containers are implements as structs?
>>
>> As the Mexican would say: why not?
>
> Well... here are a few reasons why not:
>
> Structs can't implement interfaces, and a collection API is the
> poster-child of good interface usage.
I don't want to seem like picking on you, but I would like to answer
this message too.
In my humble opinion, collections APIs are the poster-child of the
misguided joy of the beginnings of object-orientation, right next to the
mammal isa animal example. Organizing collections in hierarchies is of
occasional benefit, but the real benefit comes from decoupling
containers from algorithms via iterators, the way the STL did. The
savings there were that an O(m * n) problem has been solved with writing
O(m + n) code. Container hierarchies do not achieve such a reduction
unless efforts are being made that have little to do with hierarchical
organization. At most they factor out some code in the upper classes,
but hierarchies help when types have many commonalities and few
difference, and that is very rarely the case with containers.
> Structs can't inherit from abstract classes. A good deal of
> functionality in a collection class can be implemented once in an
> abstract base class, making it simpler for subclass authors to implement
> the API without duplicating a bunch of boilerplate.
This also reveals a beginner's view of inheritance. You don't inherit to
reuse. You inherit to be reused. If all you want is to reuse, use
composition.
> Conceptually -- and there's no hard and fast rule about this -- structs
> usually either represent small logically-atomic values (DateTime), or
> some fixed-size collection of logically-atomic values (Vector3). Using a
> struct as an arbitrarily-sized container seems, on the face of it, to
> break those conventions.
I see how it breaks those conventions, but I've never heard of them. The
convention I heard of is that if you want dynamic polymorphism you use
classes, otherwise you don't.
Andrei
More information about the Digitalmars-d
mailing list