\section{Nesting of generic function calls}

The Law of Demeter has implications for the nesting of function
calls. To discuss those implications, we classify  functions as follows:

\begin{definition}
\begin{itemize}
\item
An accessor function returns an object which existed before the
function was called.
\item
A constructor function returns an object which did not exist before
the function was called.
\end{itemize}
\end{definition}

The Law allows the nesting of constructor functions, as well as
the nesting of non-generic functions (if a multi-paradigm language is 
involved). 
A generic function does not necessarily have to contain
an explicit 'make-instance' call (or its equivalent) to cause the
creation of a new object.  If the generic function has within it
a call to a function which returns {\em a newly created object} then this
too is considered a creation of an object within the generic function.

However, the Law restricts functional composition of accessor functions.
As an example, consider the following list processing example which the
Law would consider to be in bad style: 
(We use the CLOS notation in this section.)

\bv
;;; Number-List ~ {Number}.
(defmethod third ((x Number-List))
  (car (cdr (cdr x))))
\end{verbatim}
We view this in bad style for two reasons. First, the writer of the
method assumes that there are at least three elements in the list.
If this is indeed the case, it might have been better to define the
list as follows.
\bv
;;; New = <salary> Number <age> Number 
;;;   <third> Number <rest> Number-List.
\end{verbatim}
Then selecting the third element is much easier (more intuitive and
less error prone).

The second reason is that it is not good to ``dig'' into structures.
We choose another example to demonstrate this point:

\bv
;;; P0 = <p1> P1.
;;; P1 = <p2> P2.
;;; P2 = <p3> P3.
(defmethod get-p3 ((x P0))
  (p3 (p2 (p1 x))))
\end{verbatim}
A better solution is:
\bv
(defmethod get-p3 ((x P0))
  (get-p3 (p1 x))
(defmethod get-p3 ((x P1))
  (p3 (p2 x)))
\end{verbatim}
If we update class {\tt P1} (say instead of p2 we have two other instance
variables), in the first case we have to modify
the triply nested method {\tt get-p3}
attached to class {\tt P0} (although the definition of 
{\tt P0} did not change). 
In the second case, we only have to update the method attached to {\tt P1}.

In the Number-List example we could not make the same point
since cdr returns another object of the Number-List class. 
The compile-time checkable version of the Law of Demeter
would allow
the  nesting of accessor functions, if their return type is not
a ``new'' type, that is, if it is an instance variable or an argument
type.  Thus, the class formulation of the Law is less restrictive in this
case than the object formulation. 


%The following illustrates the valid use of nested constructor functions.
%The following nesting is in good style:
%\bv
%        (defmethod good-style ((x float) (y float)) 
%	  (+ (sqrt (- x y)) y))
%\end{verbatim}
%
%since the functions +, -, and sqrt are all constructor functions.
%The result of applying the built-in generic function - is an argument
%to the sqrt generic function, as is the result of sqrt to +.
%The result of applying - and sqrt
%is generated within the function body itself.
%
%In other words,
%the application of the '-' to the x and y causes the creation of a new
%instance of the float class which is initialized to a value representing
%'x - y'. So calling '-' is just the same as creating a new object  and
%giving the instance variables of that object some initial values depending
%on the arguments and some procedural processing.  
%%Thus the argument to the
%%sqrt function is again valid.
%
%
%%	As a less trivial example, consider the following. In Unix, the
%%pipe construct has the semantics of functional composition. Thus, to
%%format a troff document and send it to the line printer the following
%command line is used:
%
%\bv
%troff <switches> <filename> | lpr -P<printer>
%\end{verbatim}
%Using functional composition in Lisp/CLOS, we have:
%\bv
%(defmethod output-document 
%    ((switches string) 
%     (filename pathname) 
%     (printer string))
%  (lpr printer (troff switches filename)))
%(defmethod lpr 
%    ((printer string) 
%     (document stream))
%  <<code to send document to the printer>> )
%
%(defmethod troff 
%  ((switches string) 
%   (filename pathname))
%  <<code to do troff, 
%    might be in Lisp or a call to Unix>>)
%\end{verbatim}
%
%The method output-document is in good style since troff is a constructor
%function which returns a new stream object.
%
%%	The functional programming style is a useful and powerful one,
%%though, as with all programming styles, it has its limitations. One of
%%the advantages of CLOS is that it makes a combination of functional
%%and object-oriented styles possible, allowing side effect producing
%%operations to be limited in extent, which, in turn, makes proofs of
%%correctness easier. By eliminating functional composition for generic
%%functions, the Law of Demeter puts unnecessary restrictions on the
%%programmer.
%It is important to note that we haven't ``thrown out the baby with the
%bathwater'' with this interpretation of ``object creation''.  
%A generic function 
%is allowed to be dependant on 
%a limited set of objects and their
%types, but is not allowed to be dependent on the sub-parts of those objects.
%The deep structures of the objects are hidden from the generic function.
%This is a real and
%important restriction on the programmer.

