How do record variants without a field name?

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

How do record variants without a field name?

Ryan Gonzalez
I posted this to Stack Overflow (http://stackoverflow.com/questions/36504292/iso-pascal-record-variants-without-a-field-name), but it hasn't gotten any feedback. So, here I go...

Free Pascal allows you to do this:

type RPoint = Record
  Case Boolean of
    False : (X,Y,Z : Real);
    True : (R,theta,phi : Real);
end;

To construct it, you do:

var p: RPoint;
begin
  p.x := 1;
end.

There's one part I don't understand: what's the purpose of the Case Boolean part? I understand that you can do case MyVal: Boolean; then MyVal becomes the field selector. However, what is the purpose when there is no field selector, just a type?

In addition, the ISO Pascal standard says:

With each variant-part shall be associated a type designated the selector-type possessed by the variant-part . If the variant-selector of the variant-part contains a tag-field, or if the case-constant- list of each variant of the variant-part contains only one case-constant, then the selector-type shall be denoted by the tag-type, and each variant of the variant-part shall be associated with those values specified by the selector-type denoted by the case-constants of the case-constant-list of the variant . Otherwise, the selector-type possessed by the variant-part shall be a new ordinal-type that is constructed to possess exactly one value for each variant of the variant-part, and no others, and each such variant shall be associated with a distinct value of that type.

I don't quite understand what the selector-type is and why it would be a new ordinal-type. Wouldn't the selector-type just be the type like in case Boolean of? And what does each case-constant-list having only one case-constant have to do with it?

Thanks in advance!

--
Ryan
[ERROR]: Your autotools build scripts are 200 lines longer than your program. Something’s wrong.

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: How do record variants without a field name?

leledumbo
Administrator
> I posted this to Stack Overflow (http://stackoverflow.com/questions/36504292/iso-pascal-record-variants-without-a-field-name), but it hasn't gotten any feedback

There are 2 answers already ATM, so I won't add the 3rd one (hard to get upvote :p)

> what's the purpose of the Case Boolean part?

That's what makes variant record different from ordinary record.

> I understand that you can do case MyVal: Boolean; then MyVal becomes the field selector. However, what is the purpose when there is no field selector, just a type?

The same, it's useful when you have no need to check which variant is active ATM. I seldom do this, I always think the selector can be useful someday.

> I don't quite understand what the selector-type is

It's in the first sentence: "With each variant-part shall be associated a type designated the selector-type possessed by the variant-part."
Just imagine there is this selector-type owned by variant-part, whose meaning is decided later because it depends on conditions explained afterwards.

> why it would be a new ordinal-type

because multiple constant values in the case-constant-list does not have a type yet, so it's a new ordinal type when used.

> Wouldn't the selector-type just be the type like in case Boolean of?

yes and no, see above.

> And what does each case-constant-list having only one case-constant have to do with it?

When only one case-constant is used, there's no confusion, each of the constant will of course should be in the range of tag-type. Hence, no new type required to be constructed.