home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.pascal
- Path: sparky!uunet!sun-barr!cs.utexas.edu!torn!news.ccs.queensu.ca!dmurdoch
- From: dmurdoch@QueensU.CA (Duncan Murdoch)
- Subject: Constant objects in TP 6.0
- Message-ID: <Bs7qyv.KK3@knot.ccs.queensu.ca>
- Sender: news@knot.ccs.queensu.ca (Netnews control)
- Organization: Queen's University, Kingston, Canada
- Date: Thu, 30 Jul 1992 17:52:07 GMT
- Lines: 57
-
-
- I've just discovered a problem (I'm not sure if I'd call it a bug, but a
- design inconsistency) in Turbo Pascal 6.0's handling of constant objects.
-
- With the following declaration:
-
- type
- Tbase = object
- private
- i : word;
- end;
-
- it's possible to declare a typed constant of type TBase in another unit:
-
- const
- base : TBase = (); { 1 }
-
- However, if we create a descendant type:
-
- type
- TDescend = object(TBase)
- j : word;
- end;
-
- then there's no way to create a constant of type TDescend which
- initializes j. The first possibility
-
- const
- descend : TDescend = (i:1; j:1); { 2 }
-
- is illegal outside the unit where TBase was defined, because field "i"
- can't be referenced (it's private). The second possibility
-
- const
- descend : TDescend = (j:1); { 3 }
-
- is illegal because j isn't the first field; you're not allowed to skip
- field i.
-
- The reason I'm reluctant to call this a bug is because it makes some
- sense. Field "i" is private, so { 2 } can't touch it in another unit; the
- compiler doesn't know what value to put there, so it's good that it refuses
- to let you use { 3 }. If anything's a bug, it's the fact that { 1 } is
- legal. Apparently you're always allowed to specify the first few fields
- in an object or record, and the others are zeroed out.
-
- I'd suggest the following alternative syntax for constant objects:
-
- type
- descend : TDescend.Init(...);
-
- where Init is a constructor. This would be executed just before the
- initialization section of the unit it occured in. The constructor could
- set any private fields and generally initialize the object properly.
-
- Duncan Murdoch
- dmurdoch@mast.queensu.ca
-