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

StackOverflow https://stackoverflow.com/questions/2072883

  •  20-09-2019
  •  | 
  •  

Question

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?

Was it helpful?

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top