Monday, July 04, 2005

The C++ nesting of type definitions

In programming language Pascal and its derivatives like ADA one can nest the definition of a function inside another function. The view here is that, the entire program is a function, and therefore this nesting is just a form of recursion.

In C and its derivatives like C++, instead of functions one can nest definitions of types inside a structure, or a class for that matter. This is also considered a form of recursion.

In hiding a function inside another, one can think of two functions that need to call a function with exact same name and signature but for different purposes. This is quite insignificant as an abstraction for a software development language.

In C the mechanism for nesting un-named unions inside a structure has some low level uses, which can be done just as easily without nesting. Furthermore, the nesting of type definitions is just as insignificant as that of functions. Here too, one is dealing with scopes in that access to a type defined inside a class requires the class identifier as a qualifier. The fact that types can be nested in private section of a class is of little use, if any. After all the use of the nested type does not affect the class that hides the type in any way.

C++, due to its weak notion of namespaces, uses its class type construction mechanism to hide the definition of other types. For instance, one frequently finds an enumeration defined in the private section of a class. In fact, specifying all methods of a class to be static effectively turns the class into a namespace.

A type is an abstraction of some entity with certain characteristics. An object as an instance of the type is a virtual representation of the entity being abstracted. One should not confuse the purpose of linguistic abstractions for software development with ad hoc mechanisms for producing programs as bundles of hacks and tricks. A nested type distorts the logical view that a type is an abstraction of some entity because the nesting is related to the notion of scope, not the mapping of an entity to a type.

The notion of class as a mechanism for defining new types parallels the notion of abstract space in mathematics for defining new mathematical spaces and algebraic structures. Thus, it is more logical to evolve the notion of class by adding invariants and constraints. This is the approach taken by Z++.

Labels: ,