Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2016-05

How to declare function with untyped array parameter

8 messages · 3 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

LacaK

Hi,

is there way how to declare function, which will accept as parameter any array type ?
(this parameter I need forward to System.Length() only )

Just like Length() function.

Now I fall into problem with generics , where I have in one unit generic object, which has Length property (or Length function) This function internally calls System.Length(A)

But when I specialize such generic in another unit, I give "Compilation aborted" (FPC 2.6.4)

Culprit is prefix "System.".

When I omit prefix "System." then I give another error: Wrong number of parameters specified for call to "Length"
(compiler is probably recognizing "Length" of object )

Example:

-----------------------------------------------------------------------------

1st unit:

   generic GT<T> = object
     private
       var
         FArray: array of T;
         Fx, Fy: integer;
       function GetLength: integer;
     public
       type
         TElement = T;
         PElement = ^T;
       constructor Init; overload;
       constructor Init(x,y: integer); overload;
       property Length: integer read GetLength;
   end;
function T2DArray.GetLength: integer;
begin
   Result := System.Length(FArray);   // <-- HERE prefix System is
causing "Compilation aborted" in FPC 2.6.4
end;

-----------------------------------------------------------------------------

2nd unit:

T2DIntArray = object(specialize GT<Integer>) end;

-----------------------------------------------------------------------------

Is there any workaround ?

Thanks

L.

Sven Barth
to LacaK
"LacaK" wrote
Hi, is there way how to declare function, which will accept as parameter any

array type ?

Show quoted text 5 lines
(this parameter I need forward to System.Length() only ) Just like Length() function. Now I fall into problem with generics , where I have in one unit generic

object, which has Length property (or Length function)

This function internally calls System.Length(A) But when I specialize such generic in another unit, I give "Compilation

aborted" (FPC 2.6.4)
[snip]

Is there any workaround ?

Test with 3.0.0. Everything before that is vastly inferior regarding generics.
If it happens there and with 3.1.1 as well then please file a bug report.

Regards,
Sven

Sven Barth
to Marco van de Voort
"Marco van de Voort" wrote 8 lines
In our previous episode, LacaK said: > 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.

In that specific case that might not really help except if one puts the code into a class function of another generic class that does not have a Length method or property. But even then it might still fail cause of insufficient support in 2.6.4.

Regards,
Sven

LacaK
to Sven Barth: array type ? object, which has Length property (or Length function) aborted" (FPC 2.6.4)…

Dňa 26.5.2016 o 9:11 Sven Barth napísal(a):

Show quoted text 25 lines
Am 26.05.2016 08:33 schrieb "LacaK" < >: > > Hi, > > is there way how to declare function, which will accept as parameter any array type ? > (this parameter I need forward to System.Length() only ) > > Just like Length() function. > > Now I fall into problem with generics , where I have in one unit generic object, which has Length property (or Length function) > This function internally calls System.Length(A) > > But when I specialize such generic in another unit, I give "Compilation aborted" (FPC 2.6.4) [snip] > Is there any workaround ? Test with 3.0.0. Everything before that is vastly inferior regarding generics. If it happens there and with 3.1.1 as well then please file a bug report.

AFAICS with 3.0 it works
(but I need 2.6.4 for now)
Thanks
-Laco.

LacaK
to Marco van de Voort: In our previous episode, LacaK said: Generally one uses TArray<T> as parametertype. in…
Show quoted text 5 lines
> 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()

Sven Barth
to LacaK
"LacaK" wrote 7 lines
>> 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

Show quoted text 13 lines
> 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

LacaK
to Sven Barth
Show quoted text 27 lines
> > >>> 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()

Yes

Show quoted text 9 lines
. Try something like this: === code begin === type generic TMyHelper<T> = class class function GetLength(aArray: specialize TArray<T>): LongInt; end;

When I use:

   generic TLengthHelper<T> = class
     class function GetLength(A: specialize T2DArray<T>): Integer;  //
<-- HERE I get error "Error: expected ;, but T2DArray found"
   end;

Ok, I will leave "Length" and rename it to "Count" ;-)

Thanks
-Laco.