[fpc-pascal] private type and type compatibility

Michael Van Canneyt michael at freepascal.org
Wed Oct 30 12:50:39 CET 2013



On Wed, 30 Oct 2013, Jonas Maebe wrote:

>
> On 30 Oct 2013, at 12:09, Sven Barth <pascaldragon at googlemail.com> wrote:
>
>> Am 30.10.2013 11:59, schrieb Jonas Maebe:
>>>
>>> The tdynarray type is not visible in the program because u1 is not in its uses clause (it's not in scope whatsoever), and nevertheless there is no problem to use it. It's of course not exactly the same (tdynarray isn't declared as private to u1), but at the scope visibility level it is the same situation as far as I am concerned.
>> Hmm... but here the compiler can not know whether the unit using u2 has unit u1 in scope or not. In case of private and protected types the compiler can know however that code declared externally can never have access to it
>
> It can, as demonstrated by the example that started this thread. The type identifier may not be visible, but entities that have this type can be. Just like in the unit example.

You must admit that in the case of a function result type, that is a bit awkward, 
since you will never be able to declare a properly typed variable to hold the function 
result. In the case of a record or class type, that is particularly awkward since you 
will be forced to use a "with" to access the various fields.

Type
   TMyClass = Class
   private
    type myrec = record
     a,b : SomeOtherType;
     end;
   public
     function MyFunc : Myrec;
   end;

You cannot do

Var
   c : TMyClass;
   r : myrec; // will fail.

begin
   r:=c.myfunc;
   Writeln(r.a,' ',r.b);
end;

you can only do

Var
   c : TMyClass;

begin
   with c.myfunc do
     Writeln(r.a,' ',r.b);
end;

Michael.



More information about the fpc-pascal mailing list