[fpc-pascal] How to declare function with untyped array parameter
Sven Barth
pascaldragon at googlemail.com
Thu May 26 11:52:45 CEST 2016
Am 26.05.2016 10:38 schrieb "LacaK" <lacak at zoznam.sk>:
>
>
>>> is there way how to declare function, which will accept as parameter any
>>> array type ?
>>> (this parameter I need forward to System.Length() only )
>>
>> Generally one uses TArray<T> as parametertype. in such cases. One can
take
>> the length of a generic array.
>
> Hm,
> I do not understand.
>
> I need something like:
>
> function GetLength(const A): integer; inline;
> begin
> Result := System.Length(A); // of course here I get "Type mismatch"
> end;
>
> Regular function, which I will call from generic object method as far as
inside generic object method I can not use System.Length()
Your problem is that you have a Length property in your class, so that's
why you need to use System.Length(). Try something like this:
=== code begin ===
type
generic TMyHelper<T> = class
class function GetLength(aArray: specialize TArray<T>): LongInt;
end;
// in your object you do this:
type
THelper = specialize TMyHelper<T>;
// and in your GetLength method you use THelper.GetLength(YourArray);
=== code end ===
Not tested, it might not compile as is (maybe you need to tweak around a
bit).
In general I can't recommend anything before 3.0.0 for serious use of
generics.
Regards,
Sven
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-pascal/attachments/20160526/0bed690f/attachment.html>
More information about the fpc-pascal
mailing list