开发者

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 t开发者_开发百科he advantages of doing so?

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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜