home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #30 / NN_1992_30.iso / spool / comp / lang / perl / 7547 < prev    next >
Encoding:
Internet Message Format  |  1992-12-21  |  4.8 KB

  1. Path: sparky!uunet!eiffel!eiffel.com
  2. From: ram@eiffel.com (Raphael Manfredi)
  3. Newsgroups: comp.lang.perl
  4. Subject: Re: Package & Namespace Question
  5. Summary: Every package needs to inherit from main
  6. Keywords: package, inheritance, perl 5
  7. Message-ID: <163@eiffel.eiffel.com>
  8. Date: 19 Dec 92 01:40:12 GMT
  9. References: <BzD4Hw.AG8@da_vinci.it.uswc.uswest.com> <1992Dec17.210221.21875@netlabs.com>
  10. Sender: ram@eiffel.com
  11. Organization: Interactive Software Engineering, Santa Barbara CA
  12. Lines: 94
  13.  
  14. Quoting lwall@netlabs.com (Larry Wall) from comp.lang.perl:
  15. >The primary motivation for nesting packages would be to provide inheritance
  16. >of object methods, and I don't see the need offhand for every class to
  17. >derive from package main.  If you've got a good reason to do so, now is
  18. >the time to let me know.
  19.  
  20. I believe it is a desirable property to have all the classes (packages)
  21. inherit from one root class (package main), since the root class is the
  22. only possible repository for basic features which need to be seen by all
  23. the user-defined classes.
  24.  
  25. Let me give you an example by using Eiffel terminology and syntax, since
  26. Perl does not (yet) support OO constructs. Those of you who are not interested
  27. in those considerations may skip to the next article (needless to say, since
  28. if you came that far...).
  29.  
  30. So, in Eiffel 3, all the classes are implicitely inheriting from one class
  31. called ANY, which means all the possible types in the system (a class is
  32. both a module and a type in Eiffel) conform to ANY. (Eiffel purists will know
  33. that I am an odious liar, since I am omitting class GENERAL, but this is not
  34. comp.lang.eiffel).
  35.  
  36. This means that if you want to declare a fairly general data structure (say a
  37. LINKED_LIST), you can declare:
  38.  
  39.     pool: LINKED_LIST [ANY];
  40.  
  41. (The [] denotes genericity, pool is a linked list whose items are instances
  42. -- or heirs for that matter -- of ANY).
  43.  
  44. You will be able to put *any* object into that structure, since every class
  45. is inheriting from ANY and therefore any object can be assigned to an
  46. entity whose type is ANY (Eiffel is "strongly" typed).
  47.  
  48. The ANY class also defines some common feature, for instance the 'twin'
  49. feature which, applied to an object, returns a shallow copy (a clone) of
  50. that object. In Eiffel, it is declared (by association) as:
  51.  
  52.     twin: like Current is
  53.             -- New object field-by-field identical to `Current'.
  54.         do
  55.             ...
  56.         ensure
  57.             is_equal: Result.is_equal (Current);
  58.         end;
  59.     
  60. (The ensure clause is the post-condition, and it makes it pretty obvious
  61. what `twin' does -- Current is the instance on which twin is called, and
  62. Result is the predefined entity used to hold the function result).
  63.  
  64. Declaring `twin' by association is actually useful, since a given class C,
  65. inheriting from ANY implicitely, twin will be visible and will return something
  66. like C, i.e. either a C or one of its heirs.
  67.  
  68. Some special classes may chose to *redefine* twin by explicitely inheriting
  69. from ANY and redefining that feature, which means dynamic binding will
  70. apply and select the right implementation. Indeed, some basic features may
  71. be more efficiently implemented in heirs (class INTEGER comes to mind).
  72.  
  73. If INTEGER was not actually inherting from ANY, there would be no way for
  74. this class to re-implement the basic `twin' facility, which for in integer
  75. is really a delicate matter, since it is a basic type, and has "expanded"
  76. semantic in Eiffel (implying a semantic of copy on attachement).
  77.  
  78. Ok, back to Perl.
  79.  
  80. I know Perl has no pretention of being a strong OO language, but still there
  81. are some single concepts like inheritance which, taken to their limits, can
  82. produce outstanding results. In particular, grouping common feature in a
  83. global package from which all the package would inherit (e.g. exception
  84. handling) would give a tremendous amout of flexibility.
  85.  
  86. Of course, simply inheriting is not enough. The ability to rename and also
  87. redefine a single routine has to be given, along with automatic dynamic
  88. binding (possibly optimized by the compiler as static binding if no
  89. redefinition occurs -- that's better than having to explicitely request
  90. for dynamic binding since this enhances reusability).
  91.  
  92. This also means that multiple inheritance must be supported to some extent,
  93. and, possibly, repeated inheritance (i.e. inheriting more than once from one
  94. particular package -- this can be direct or indirect via multiple inheritance).
  95.  
  96. Anyway, it all depends to what degree you want to support Object-Oriented
  97. features. But a fair amount of generality is best, since it leaves the
  98. door open to future extensions. So if it does not cost much to let
  99. every package inherit from main, why not make it available. Further extensions
  100. will come naturally.
  101.  
  102. Now, did I do a good job in convincing you?
  103. -- 
  104. Raphael Manfredi <ram@eiffel.com>
  105. Interactive Software Engineering Inc.
  106. 270 Storke Road, Suite #7                      / Tel +1 (805) 685-1006 \
  107. Goleta, California 93117, USA                  \ Fax +1 (805) 685-6869 /
  108.