Sign in Join

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

Mailing lists / fpc-pascal / 2013-04

Correct way for partial generic specialization

7 messages · 2 people · –

Reply by email Subscribe to this list Your subscription

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

leledumbo

This code:

{$mode objfpc}{$H+}
type
  generic TType1<A,B> = class end;
  generic TType2  = specialize TType1<A,String>;
begin
end.
fails:
x.pas(5,42) Error: Identifier not found "A"
x.pas(5,42) Error: Type identifier expected
x.pas(5,50) Error: Illegal expression
x.pas(7,1) Error: This type can't be a generic

but this code:

{$mode objfpc}{$H+}
type
  generic TType1<A,B> = class end;
  generic TType2  = class(specialize TType1<A,String>) end;
begin
end.

works. Does it have to be like the 2nd code for partial generic specialization?

View this message in context: http://free-pascal-general.1045716.n5.nabble.com/Correct-way-for-partial-generic-specialization-tp5714160.html Sent from the Free Pascal - General mailing list .
Sven Barth
to leledumbo: This code: {$mode objfpc}{$H+} type generic TType1<A,B> = class end; generic TType2 =…
"leledumbo" wrote 18 lines
This code: {$mode objfpc}{$H+} type generic TType1<A,B> = class end; generic TType2 = specialize TType1<A,String>; begin end. fails: x.pas(5,42) Error: Identifier not found "A" x.pas(5,42) Error: Type identifier expected x.pas(5,50) Error: Illegal expression x.pas(7,1) Error: This type can't be a generic

As the compiler tells you: A is not a defined type. The compiler needs to know the complete type before it can use any type defined in there and in this case the full type is "TType1<A, String>" where both types must come from outside the type.

Show quoted text 13 lines
but this code: {$mode objfpc}{$H+} type generic TType1<A,B> = class end; generic TType2 = class(specialize TType1<A,String>) end; begin end. works. Does it have to be like the 2nd code for partial generic specialization?

This must only work if you write "generic TType2<A> =...". Otherwise this is a bug.

One can argue however that the following should be valid as well (AFAIK it is not currently):

=== example begin ===

type
  generic TType2<A> = specialize TType1<A, String>;

=== example end ===

Regards,
Sven

leledumbo
to Sven Barth
This must only work if you write "generic TType2 =...". Otherwise this is

a bug.

The code should have it, I missed it when copypaste-ing.

One can argue however that the following should be valid as well (AFAIK it is not currently):

Yes, this is my original question. Let me rephrase:

Having this declaration:

type
  generic TType1<A,B> = class end;

Why the following is not valid:

  generic TType2  = specialize TType1<A,String>;

but this one is:

  generic TType2  = class(specialize TType1<A,String>) end;

?

Is it possible to do partial generic specialization without the need of inheritance?

View this message in context: http://free-pascal-general.1045716.n5.nabble.com/Correct-way-for-partial-generic-specialization-tp5714160p5714169.html Sent from the Free Pascal - General mailing list .
Sven Barth
to leledumbo
"leledumbo" wrote
> One can argue however that the following should be valid as well (AFAIK

it

Show quoted text 12 lines
> is not currently): Yes, this is my original question. Let me rephrase: Having this declaration: type generic TType1<A,B> = class end; Why the following is not valid: generic TType2 = specialize TType1<A,String>;

You're missing the <A> again. :)

Show quoted text 8 lines
but this one is: generic TType2 = class(specialize TType1<A,String>) end; ? Is it possible to do partial generic specialization without the need of inheritance?

Not currently. It might be a useful extension however. I'll need to test what Delphi says about this...

Regards,
Sven

Sven Barth
to leledumbo
leledumbo wrote
> You're missing the again. :) *self-headcut

Can it be that your E-Mail client misinterprets the "<...>" (with only one string as "value") as HTML tags? Because I can't see the "<A>" I had written to you in your quotation.

> Not currently. It might be a useful extension however. I'll need to test > what Delphi says about this... OK, I'll wait

So... Delphi does not support that, but I still think that this is a valid extension of generics and can be used for all generic-able types instead of only classes.

Regards,
Sven