home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #16 / NN_1992_16.iso / spool / comp / lang / pascal / 4623 < prev    next >
Encoding:
Text File  |  1992-07-30  |  2.0 KB  |  68 lines

  1. Newsgroups: comp.lang.pascal
  2. Path: sparky!uunet!sun-barr!cs.utexas.edu!torn!news.ccs.queensu.ca!dmurdoch
  3. From: dmurdoch@QueensU.CA (Duncan Murdoch)
  4. Subject: Constant objects in TP 6.0
  5. Message-ID: <Bs7qyv.KK3@knot.ccs.queensu.ca>
  6. Sender: news@knot.ccs.queensu.ca (Netnews control)
  7. Organization: Queen's University, Kingston, Canada
  8. Date: Thu, 30 Jul 1992 17:52:07 GMT
  9. Lines: 57
  10.  
  11.  
  12. I've just discovered a problem (I'm not sure if I'd call it a bug, but a
  13. design inconsistency) in Turbo Pascal 6.0's handling of constant objects.
  14.  
  15. With the following declaration:
  16.  
  17. type
  18.   Tbase = object
  19.     private
  20.       i : word;
  21.   end;
  22.  
  23. it's possible to declare a typed constant of type TBase in another unit:
  24.  
  25. const
  26.   base : TBase = ();    { 1 }
  27.  
  28. However, if we create a descendant type:
  29.  
  30. type
  31.   TDescend = object(TBase)
  32.     j : word;
  33.   end;
  34.  
  35. then there's no way to create a constant of type TDescend which
  36. initializes j.  The first possibility
  37.  
  38. const
  39.   descend : TDescend = (i:1; j:1);  { 2 }
  40.  
  41. is illegal outside the unit where TBase was defined, because field "i"
  42. can't be referenced (it's private).  The second possibility
  43.  
  44. const
  45.   descend : TDescend = (j:1);  { 3 }
  46.  
  47. is illegal because j isn't the first field; you're not allowed to skip
  48. field i.
  49.  
  50. The reason I'm reluctant to call this a bug is because it makes some
  51. sense.  Field "i" is private, so { 2 } can't touch it in another unit; the
  52. compiler doesn't know what value to put there, so it's good that it refuses
  53. to let you use { 3 }.  If anything's a bug, it's the fact that { 1 } is
  54. legal.  Apparently you're always allowed to specify the first few fields
  55. in an object or record, and the others are zeroed out.
  56.  
  57. I'd suggest the following alternative syntax for constant objects:
  58.  
  59. type
  60.   descend : TDescend.Init(...);
  61.  
  62. where Init is a constructor.  This would be executed just before the
  63. initialization section of the unit it occured in.  The constructor could
  64. set any private fields and generally initialize the object properly.
  65.  
  66. Duncan Murdoch
  67. dmurdoch@mast.queensu.ca
  68.