Sign in Join

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

Mailing lists / fpc-pascal / 2013-09

procedure that accept generic types?

6 messages · 2 people ·

Reply by email Subscribe to this list Your subscription

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

Xiangrong Fang

Hi There,

I try to write a procedure with generic parameters, but failed:

type
  generic TArray<T> = array of T;
procedure ProcessArray(arr: TArray<T>);
begin
end;

The compiler said "Generics without specialization cannot be used..."

But if I specialize the parameter then this procedure is useless.

Any ideas?

Thanks

Sven Barth
to Xiangrong Fang
Xiangrong Fang wrote 18 lines
Hi There, I try to write a procedure with generic parameters, but failed: type generic TArray<T> = array of T; procedure ProcessArray(arr: TArray<T>); begin end; The compiler said "Generics without specialization cannot be used..." But if I specialize the parameter then this procedure is useless. Any ideas?

This is not yet supported. A proof of concept implementation exists locally on my computer however. It will then look like this:

=== code begin ===

type
   generic TArray<T> = array of T;
generic procedure ProcessArray<T>(arr: TArray<T>);
begin
end;
begin
   // usage:
   specialize ProcessArray<LongInt>(someintarray);
end.

=== code end ===

Regards,
Sven

Xiangrong Fang
to Sven Barth

Thanks. This is sort-of too verbose when use. I have no comments about how to define it, but when use it, can we just use:

ProcessArray(someintarray); ?

The complier should be able to "specialize" it implicitly because it knows someintarray is array of integer?

alternatively, how about this:

var
  proc_ints: specialize ProcessArray<LongInt>;
begin
  proc_ints(someintarray);
end;

use it without explicit specialize is best, however.

2013/9/4 Sven Barth

Show quoted text 47 lines
Am 04.09.2013 08:18, schrieb Xiangrong Fang: Hi There, I try to write a procedure with generic parameters, but failed: type generic TArray<T> = array of T; procedure ProcessArray(arr: TArray<T>); begin end; The compiler said "Generics without specialization cannot be used..." But if I specialize the parameter then this procedure is useless. Any ideas? This is not yet supported. A proof of concept implementation exists locally on my computer however. It will then look like this: === code begin === type generic TArray<T> = array of T; generic procedure ProcessArray<T>(arr: TArray<T>); begin end; begin // usage: specialize ProcessArray<LongInt>(someintarray); end. === code end === Regards, Sven _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Sven Barth
to Xiangrong Fang
Xiangrong Fang wrote
Thanks. This is sort-of too verbose when use. I have no comments about how to define it, but when use it, can we just use: ProcessArray(someintarray); ?

No. Generics are specialized with "<SomeType>", so not using it for procedures/functions/methods as well is inconsistent.

Show quoted text 14 lines
The complier should be able to "specialize" it implicitly because it knows someintarray is array of integer? alternatively, how about this: var proc_ints: specialize ProcessArray<LongInt>; begin proc_ints(someintarray); end; use it without explicit specialize is best, however.

No. In non-Delphi modes specializations require "specialize". This is for consistency of the language and also for simplification of the parser in those modes.

And just in case: The syntax is not up to debate.

Regards,
Sven

Xiangrong Fang
to Sven Barth

Hi Sven,

Not to "debate" the syntax, but just ask :-)

1) in my second example: proc_ints: specialize ProcessArray<LongInt>; it is "specialized". Why this is still not consistent ?

2) in delphi mode, "specialize" is not required anywhere, right? If so, can you write an example of your new proposal in delphi mode? btw, my last experience with Delphi is 6 or 7, there was no generics yet.

Thanks

2013/9/4 Sven Barth

Show quoted text 38 lines
Am 04.09.2013 09:13, schrieb Xiangrong Fang: Thanks. This is sort-of too verbose when use. I have no comments about how to define it, but when use it, can we just use: ProcessArray(someintarray); ? No. Generics are specialized with "<SomeType>", so not using it for procedures/functions/methods as well is inconsistent. The complier should be able to "specialize" it implicitly because it knows someintarray is array of integer? alternatively, how about this: var proc_ints: specialize ProcessArray<LongInt>; begin proc_ints(someintarray); end; use it without explicit specialize is best, however. No. In non-Delphi modes specializations require "specialize". This is for consistency of the language and also for simplification of the parser in those modes. And just in case: The syntax is not up to debate. Regards, Sven _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Sven Barth
to Xiangrong Fang
Xiangrong Fang wrote 6 lines
Hi Sven, Not to "debate" the syntax, but just ask :-) 1) in my second example: proc_ints: specialize ProcessArray<LongInt>; it is "specialized". Why this is still not consistent ?

You declared "proc_ints" as a variable and specialized a procedure. That is not correct from the typing point of view. We'd either need a new section type for procedure/function specializations or we ensure that the following works:

=== code begin ===
type
   generic TArrayProc<T> = procedure(arr: TArray<T>);
var
   proc_ints: specialize TArrayProc<LongInt> = @specialize
ProcessArray<LongInt>;
=== code end ===
2) in delphi mode, "specialize" is not required anywhere, right? If so, can you write an example of your new proposal in delphi mode? btw, my last experience with Delphi is 6 or 7, there was no generics yet.

The same just without "generic" and "specialize". This is then Delphi compatible (except the fact that Delphi does not support global generic functions/procedures, but only generic methods).

Regards,
Sven