\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
A accessor function returns an object which exists 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. 
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 prohibits functional composition of accessor functions
which don't return instance variable objects.
Indeed, the Law of Demeter,
prohibits functional composition of list processing functions.
For example, the Law of Demeter 
says that the following code is in "poor" 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: Apparently, we know that
the list has at least three elements. Therefore we should define it
as a construction class:
\bv
; New = <p1>, <p2>, <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), we have to modify in the first case
the triply nested method {\tt get-p3}
attached to class {\tt P0} (although
{\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 again an Number-List. We view this as a coincidence.
The compile-time checkable version of the Law of Demeter allows
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.

We give two more examples which use nesting of 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 has the authority to make decisions and operate
in ways which are DEPENDENT on the 'valid' method selection objects
(argument objects and slot values of method selection arguments) 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.

