views:

90

answers:

1
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 depoly this policy?

thanks.

+3  A: 

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.

Joey
seems `bondage-and-discipline`,but it does allow assignment from `integer` to `shortint`.
Jichao
Well, language designers can only think of so many cases. You can't prevent them all in the compiler. I think good old halting problem might play into that :-)
Joey
I never saw a Pascal textbook describe the language as "Bondage and Discipline". Sounds like envy from other languages :-)
Marco van de Voort
Oh, such characterization is usually frowned upon by language designers. It's just that trying to keep people from making stupid mistakes often backfires when you have to do awkwardly complex things to make them work in situations where they needed. Said constraint for example might lead to code generators using hungarian notation excessively to prevent name clashes (that would otherwise be harmless) that might be painful for people having to interface with the generated code. Not the best example, but there might be more.
Joey
+1 for using "bondage and discipline" in a technical message, and for linking to Ward's Wiki.
Wayne Conrad