pascalname-conflict

Why does pascal forbid same identifier name for method's parameter and class's data member?


type
    TPerson = class(TObject)
        name : string;
        constructor create(name : string);
    end;

would trigger compiler error.

I think a self or this pointer/reference is good enough,for its clarity. So, what are the advantages of doing so?

EDIT: One more question, could you show what are the other languages that deploy this policy?


Solution

  • To prevent name clashes where the parameter name would shadow the class member. It just can't happen this way and every name is unambiguous.

    Remember that Pascal is a bondage-and-discipline language; those are designed to try to prevent common errors.

    Another option to prevent the perceived problem is what Python does: mandate the qualification of instance members with this or self so that you have to prefix every instance member access with self.

    I don't know of any other language with that restriction, though. But some language features are indeed unique; checked exceptions for example are, too.