Search code examples
c++compiler-constructionprogramming-languagescompatibilitytypechecking

Pure name equivalence in type checking


I am reading Ravi Sethi's book about the programming language concepts, and there it says

int *i1;  
int *i2;

After these declarations, the types of i1 and i2 are not name type compatible. In a language that uses name type compatibility, variables i1 and i2 could not be compared or assigned to each other.

I wonder why are not they name compatible? They have the same name type:int. Can somebody explain this and give an example of a valid pure name equivalence? Thanks


Solution

  • Neither of them has the type int. Both are typed as pointer to int. I think Sethi's point is that in a hypothetical language using (only) name equivalence, these two pointer-to-int type expressions create two different types that are not compatible – much like two identical uses of new create distinct, non-equivalent objects.

    In a name equivalence language, you have to give a name to a type expression to use it more than once type-compatibly. In C++ syntax, that would require using typedef:

    typedef int *intp;
    intp i1;
    intp i2;
    

    Now, i1 and i2 have name-compatible types.