Version 3 of Abstract Data Types

Updated 2004-05-15 13:53:08

Google definition of the term here: [L1 ]. This page could be entitled "An argument for OO as composition of Abstract Data Types"

CMCc I'm going with this definition "A data type together with all the operations on data objects of that type that make use of information about the representation of the data type"

Array is an abstract data type, since you can access its contents with [array get] or using (), but you can't tinker with the hash collision tables which underly the array representation.

Array presents a good example of some of the benefits of encapsulation:

  • you can't change the internal representation of an array in such a way as to break it.
  • it has a clearly defined interface, represented by the subcommands of array
  • someone can change the implementation of array, without changing its behavior

Each of the other builtin data structures, and many of those in tcllib have these benefits too.

However, array and (say) ::struct::tree suffer from an inability to be composed together with other data types into more complex abstract data types.

For example, one can't derive a SortedArray, which always returns its [array get] in a predefined order. Native array doesn't do this because it's unnecessary for most uses and it would be expensive but there are cases where one might like this: for example if one was using an array to store a time sequence of events.

The ability to derive SortedArray (e.g.) from builtin Array in a coherent and consistent manner, while preserving something of the encapsulation of Array, would be a benefit of OO.

How many ways are there to wrap Array in something which adds/changes its functionality? An infinite number. There should also be a single, efficient, simple, predictable, well known way. That, I think, is what the adherents of OOTcl are suggesting. I agree with them.


RS has never felt a need for "SortedArray" - it's easy enough to iterate like this:

 foreach name [lsort [array names arr]] {...}

Types/subtypes/classes/derived classes are popular in other languages, but doing without them, I just feel freer in Tcl... :)