A Simple Library ================ In this chapter, we create a complete library that represents time and position. The ``timespace`` library provides the ``say`` generic function for all concrete classes, the mathematical function ``+`` on certain kinds of time, and the comparison functions ``<`` and ``=``, which enable users to call all other numerical comparisons, ``>``, ``>=``, ``<=``, and ``~=``. Our library consists of four files: - The LID file lists all the files in the ``timespace`` library. - The library file defines the ``timespace`` library and the ``timespace`` module. - The implementation file defines the classes, methods, and generic functions. - The test file creates instances, calls ``say`` on them, and adds time instances. We provide the test file so that you can experiment with the library. Because the test code has a purpose different from that of the implementation code, we separate them into two files. Normally, a finished library would not contain both the implementation and the test code — the test code would be in a separate library. However, when you are starting to implement your program or library, it is convenient to put all the code in one library, as we do here. We shall continue to develop the time and position library in :doc:`part2`. The complete version is given in :doc:`time-mod`. The LID file ------------ The LID file: ``timespace.lid``. .. code-block:: dylan library: timespace files: library library-implementation test The library file ---------------- The library file: ``library.dylan``. .. code-block:: dylan module: dylan-user define library timespace use dylan; use format-out; end library timespace; define module timespace use dylan; use format-out; end module timespace; The implementation file ----------------------- The implementation file: ``library-implementation.dylan``. .. code-block:: dylan module: timespace // The sixty-unit class define abstract class () slot total-seconds :: , init-keyword: total-seconds:; end class ; // decode-total-seconds define method decode-total-seconds (sixty-unit :: ) => (max-unit :: , minutes :: , seconds :: ) decode-total-seconds(abs(sixty-unit.total-seconds)); end method decode-total-seconds; define method decode-total-seconds (total-seconds :: ) => (hours :: , minutes :: , seconds :: ) let (total-minutes, seconds) = truncate/(total-seconds, 60); let (hours, minutes) = truncate/(total-minutes, 60); values(hours, minutes, seconds); end method decode-total-seconds; // encode-total-seconds define method encode-total-seconds (max-unit :: , minutes :: , seconds :: ) => (total-seconds :: ) ((max-unit * 60) + minutes) * 60 + seconds; end method encode-total-seconds; // The say generic function // Given an object, print a description of the object define generic say (any-object :: ) => (); // The time classes and methods define abstract class