Correct way for partial generic specialization

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

Correct way for partial generic specialization

leledumbo
Administrator
Reply | Threaded
Open this post in threaded view
|

Re: Correct way for partial generic specialization

leledumbo
Administrator
Sorry, I miss something:

generic TType2 = ...

:">
Reply | Threaded
Open this post in threaded view
|

Re: Correct way for partial generic specialization

Sven Barth-2
In reply to this post by leledumbo

Am 16.04.2013 16:56 schrieb "leledumbo" <[hidden email]>:
>
> 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.

> 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


_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Correct way for partial generic specialization

leledumbo
Administrator
Reply | Threaded
Open this post in threaded view
|

Re: Correct way for partial generic specialization

Sven Barth-2

Am 17.04.2013 02:11 schrieb "leledumbo" <[hidden email]>:
> > 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>;

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

> 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


_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Correct way for partial generic specialization

leledumbo
Administrator
Reply | Threaded
Open this post in threaded view
|

Re: Correct way for partial generic specialization

Sven Barth-2
Am 17.04.2013 11:11, schrieb leledumbo:
>> 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
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal