I've had this happen with DEC Pascal too.
Limiting accessible record components seems useful, but in your example, what if aType changes from Circle to Rect. Does a previously set value for Radius become inaccessible? If aType returns to Circle, is the value erased or is the previous value accessible once again?
Regards David Wood
-----Original Message----- From: Eike Lange [mailto:eike.lange@uni-essen.de] Sent: Tuesday, November 19, 2002 9:11 AM To: Subject: Probs with variant records
Hi Folks!
I'm not sure wether it's a feature or a bug but I've read, that setting a case selector in variant records makes some variants unavailable from this point. The following program demonstrates, that setting a case selector allows further overwriting of unselected variants:
program Fail3;
type TGeoType = (Circle, Rect); TGeometry = record X, Y: Integer; case aType: TGeoType of Circle: (Radius: Integer); Rect: (Width, Height: Integer) end;
var Geometry: TGeometry;
begin with Geometry do begin X := 10; Y := 20; aType := Rect; Radius := 30; Width := 40; Height := 50 end end.
Version: ======== Reading specs from /usr/local/bin/../lib/gcc-lib/i686-pc-linux-gnu/2.95.2/specs gpc version 20021111, based on gcc-2.95.2 19991024 (release)
Eike
The Information contained in this E-Mail and any subsequent correspondence is private and is intended solely for the intended recipient(s). For those other than the recipient any disclosure, copying, distribution, or any action taken or omitted to be taken in reliance on such information is prohibited and may be unlawful.
Hi!
On Tue, Nov 19, 2002 at 10:49:42AM -0000, Wood David wrote:
what if aType changes from Circle to Rect. Does a previously set value for Radius become inaccessible?
Yes, I think so.
If aType returns to Circle, is the value erased or is the previous value accessible once again?
IMHO, Radius and Width share the same Memory. If someone would change Radius, Width would change its value, independent of setting aType.
My questions are: * is my idea conform to some standards? * how _should_ GPC behave?
If there are any better examples outside, I'd like to know about them. I could live with the actual implementation of variant records, but would like to clarify this point.
Eike
Eike Lange wrote:
On Tue, Nov 19, 2002 at 10:49:42AM -0000, Wood David wrote:
what if aType changes from Circle to Rect. Does a previously set value for Radius become inaccessible?
Yes, I think so.
Yes, 6.5.3.3:
: When a variant becomes nonÂactive, all of its components shall become : totallyÂundefined.
If aType returns to Circle, is the value erased or is the previous value accessible once again?
IMHO, Radius and Width share the same Memory.
That's not guaranteed and is indeed not true in some cases in GPC. Relying on this is very unportable.
If someone would change Radius, Width would change its value, independent of setting aType.
Maybe, maybe not. It's undefined.
My questions are:
- is my idea conform to some standards?
- how _should_ GPC behave?
If there are any better examples outside, I'd like to know about them. I could live with the actual implementation of variant records, but would like to clarify this point.
AFAICS, the current implementation is ok, but we might add (optional) runtime checks in the future (which is also ok).
Frank
Hi A lot of mail on this subject covering should and could. Here's my contribution on how to avoid the problems:
There are two kinds of varient records. One is where the data is the same and the varients are different representations of that data. This "how to" is for the other kind. Three simple rules:
1. Use the tag. 2. Check the tag before accessing any varient fields. 3. Do not change the value of a tag.
Attached is a simple demo.
Russ