[fpc-pascal] with in classes/records

Martin fpc at mfriebe.de
Mon Sep 3 16:17:19 CEST 2018


On 03/09/2018 15:56, Ryan Joseph wrote:
>
> In your example if the “a” property was default than:
>
> You.Free;
>
> would be the same as:
>
> You.f.Free;
>
> right? I just don’t see where the name of the property applies. The property *removed* a name in fact. It’s like an anti-name. ;)
>

No it is not the same.

You.f.Free;
will always work, it is not ambiguous.

You.Free;
depends on no method Free being declared on the class of You, or any of its base classes, or any other default class (if more than one is allowed) that would be searched at higher priority.

You.Free;
has a risk, of suddenly and expectingly doing something else. Therefore it is not the same.
It does however take the same action, if and only if there is no other Free, but the one you wanted.

Example

TMyForm = class(TForm)
   property foo: TFoo; default;
end

TFoo has a method DoFoo. So you can do
MyForm.DoFoo

But unlike MyForm.Foo.DoFoo, the above will fail, if the LCL introduces TForm.DoFoo, which would then be used instead of your DoFoo)

Therefore the shorthand syntax can only be used if all classes are written by yourself. (And if you can trust yourself, to never add conflicting methods.)





More information about the fpc-pascal mailing list