0
$\begingroup$

I wanted to clear my doubts on the structural equivalence type check in the compilers.

Consider the following two structs (or records):

struct student {
    string sName;
    int sAge;
};
struct teacher {
    string tName;
    int tAge;
};

Are the two structs student and teacher structurally equivalent?

I have studied a recursive algorithm to check for the structural equivalence and I believe these two structs should be structurally equivalent.

However, I have also read that the types of structs are defined using the cartesian product of the data members with the data types (eg. the data type for the struct student will be something like struct((sName x string) x (sAge x int))) and thus, giving the data member names importance and differentiating between the two structs.

Also, I wonder if the order in which the data members are declared in the structs, will have anything to do with the structural equivalence i.e. are the following two structs structurally equivalent?

struct student {
    string Name;
    int Age;
};
struct teacher {
    int Age;
    string Name;
};

Please note that, here, the data member names are the same.

Any sort of help is appreciated. Thanks!

$\endgroup$
3
  • 2
    $\begingroup$ The compiler ignores the names but not the order of the fields. $\endgroup$ – Yuval Filmus May 2 at 21:10
  • $\begingroup$ Ok. So, the first two are equivalent. What about the cartesian product thing? Is that ever used by a compiler? $\endgroup$ – bigbang May 2 at 21:15
  • 3
    $\begingroup$ The Cartesian product representation is string $\times$ int. $\endgroup$ – Yuval Filmus May 2 at 21:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.