[fpc-pascal] private type and type compatibility

Sven Barth pascaldragon at googlemail.com
Wed Oct 30 12:09:20 CET 2013


Am 30.10.2013 11:59, schrieb Jonas Maebe:
>
> On 30 Oct 2013, at 10:36, Michael Van Canneyt wrote:
>
>> I think it is an error. You declare something as private, and then 
>> you use it in a public function ? If that is not a visibility clash, 
>> I don't know what is :)
>
> The ability to use types in public functions that are not necessarily 
> visible to users of that function is quite common in Pascal. 
> Otherwise, for consistency you would also have to give a compile time 
> error when compiling this:
>
> unit u1;
>
> interface
>
> type
>   tdynarray = array of integer;
>
> implementation
>
> end.
> *********
>
> unit u2;
>
> interface
>
> uses
>   u1;
>
> function f: tdynarray;
>
> implementation
>
> function f: tdynarray;
> begin
> end;
>
> end.
> **********
>
> program test;
>
> uses
>   u2;
>
> var
>   a: array of integer;
> begin
>   a:=f;
> end.
> ***
>
> 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 (though of course there might be type compatibilities like 
your dynamic array example, but most private/protected types should be 
records or classes).

Regards,
Sven



More information about the fpc-pascal mailing list