[fpc-pascal] private type and type compatibility

Sven Barth pascaldragon at googlemail.com
Wed Oct 30 10:50:47 CET 2013


Am 30.10.2013 10:36, schrieb Michael Van Canneyt:
>
>
> On Wed, 30 Oct 2013, Sven Barth wrote:
>
>> Am 30.10.2013 08:47, schrieb Xiangrong Fang:
>>       Hi All,
>>
>>       I have the following class:
>>
>>       type
>>         generic TVector<T> = class
>>         private type
>>           TDataType = array of T;
>>         private
>>           FData: TDataType;
>>           ... ...
>>         public
>>           ... ...
>>           function Slice(APos: Integer = -1; ACount: Integer = -1): 
>> TDataType;
>>         end;
>>
>> The Slice() method return a portion of the data as a dynamic array.
>>
>> My question is, as I define TDataType as PRIVATE type, why it is 
>> still usable outside of the class?  In the main program
>> I did:
>>
>> type
>>   TIntVector = specialize TVector<Integer>;
>> var
>>   iv: TIntVector;
>>   ia: array of Integer;
>> begin
>>   iv := TIntVector.Create;
>>   ... ...
>>   ia := iv.Slice;
>> end.
>>
>> This will assign a TDataType (is it TVector$TDataType or 
>> TIntVector$TDataType? I don't know) to an "array of Integer".
>> Why this assignment works?
>>
>> i.e. how does the type system work?!
>>
>> It's a bit annoying that the usage private/protected types in 
>> public/published functions is allowed... (this is however Delphi
>> compatible) Maybe this should be adjusted for non-Delphi modes for 
>> language consistency... (maybe at least as a warning which
>> could be elevated to an error if someone wants)
>
> 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 :)
I agree, but the question is should we declare this as a bug in the 
language and thus fix it which might lead to adjustments of legacy 
(2.6.x) code or use a warning. I personally would do the former (and 
better sooner than later). We need to keep the current behavior in mode 
Delphi however... (Note: Delphi additionally allows to access protected 
types from external units; could be related to the point that helper 
types allow access to protected types and fields as well...)

Regards,
Sven



More information about the fpc-pascal mailing list