Recent

Author Topic: [Solved] Inheritance with generics  (Read 4103 times)

MountainQ

  • Jr. Member
  • **
  • Posts: 65
[Solved] Inheritance with generics
« on: July 03, 2017, 09:42:50 am »
Hello everybody,

I would like to create a subclass of a generic class. However the compiler yields the following message:
"Generics without specialization cannot be used as a type for a variable"
A way around this is to use the compiler directive.
Code: Pascal  [Select][+][-]
  1. {$mode delphi}
Is there another compiler setting that allows inheritance without having to utilize the delphi mode?
The wiki page (which most likely is outdated) lists this topic as proposal.
http://wiki.freepascal.org/Generics_proposals#How_to_derive_from_the_generic_type
Thanks for your replies and apologies if the answer could easily have been obtained; if so what is a good address?
« Last Edit: July 03, 2017, 10:45:09 am by MountainQ »

Thaddy

  • Hero Member
  • *****
  • Posts: 14205
  • Probably until I exterminate Putin.
Re: Inheritance with generics
« Reply #1 on: July 03, 2017, 10:12:46 am »
You did not understand quirks mode syntax (I strongly recommend to always use delphi mode with generics) but here is the correct syntax for objfpc mode:
Code: Pascal  [Select][+][-]
  1. program inheritgenericsobjfpc;
  2. {$mode objfpc}
  3. type
  4.   generic TMyFirstClass<T> = class(Tobject)
  5.   end;
  6.  
  7.   generic TSecondClass<T> =  class(specialize TMyFirstClass<T>)
  8.   end;
  9. var
  10.   a:specialize TMyFirstClass<integer>;
  11.   b:specialize TSecondClass<string>;
  12. begin
  13.   a := specialize TMyFirstClass<integer>.Create;
  14.   a.free;
  15.   b := specialize TSecondClass<string>.Create;
  16.   b.free;
  17. end.

Trust me, you will become silly after typing specialize so many times.... :D
« Last Edit: July 03, 2017, 10:21:08 am by Thaddy »
Specialize a type, not a var.

MountainQ

  • Jr. Member
  • **
  • Posts: 65
Re: Inheritance with generics
« Reply #2 on: July 03, 2017, 10:19:36 am »
Thank you for the extremely fast reply.
Quote
You did not understand quirks mode syntax
I obviuosly do not; could you recommand a source?
Quote
(I strongly recommend to always use delphi mode with generics)
Same here: is there good reading material out there, so that I do not have to bother others.
In the meantime: thanks again; I will mark this topic as solved later this day.

Thaddy

  • Hero Member
  • *****
  • Posts: 14205
  • Probably until I exterminate Putin.
Re: Inheritance with generics
« Reply #3 on: July 03, 2017, 10:24:10 am »
https://www.freepascal.org/docs-html/current/ref/refch8.html#x102-1240008

The language reference guide.

Oh I forgot this possibility if you want to specialize beforehand and don't need to keep the hierarchy fully generic:
Code: Pascal  [Select][+][-]
  1. type
  2.   TThirdClass = specialize TMyFirstclass<integer>;

I know you already know this, but for completeness the whole shebang in proper syntax:
Code: Pascal  [Select][+][-]
  1. program inheritgenericsdelphi;
  2. {$mode delphi}
  3. type
  4.   TMyFirstClass<T> = class
  5.   end;
  6.  
  7.   TSecondClass<T> =  class(TMyFirstClass<T>)
  8.   end;
  9.  
  10.   TThirdClass = TMyFirstclass<integer>;
  11.    
  12. var
  13.   a:TMyFirstClass<integer>;
  14.   b:TSecondClass<string>;
  15.   c:TThirdClass;
  16. begin
  17.   a := TMyFirstClass<integer>.Create;
  18.   a.free;
  19.   b := TSecondClass<string>.Create;
  20.   b.free;
  21.   c.create;
  22.   c.free;
  23. end.

« Last Edit: July 03, 2017, 10:41:26 am by Thaddy »
Specialize a type, not a var.

MountainQ

  • Jr. Member
  • **
  • Posts: 65
Re: Inheritance with generics
« Reply #4 on: July 03, 2017, 10:41:07 am »
@Thaddy: Thank you one more time; that resolves the issue to me. (The reference not the 'pre-specialization')
« Last Edit: July 03, 2017, 10:44:40 am by MountainQ »

Leledumbo

  • Hero Member
  • *****
  • Posts: 8746
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Inheritance with generics
« Reply #5 on: July 03, 2017, 11:02:51 am »
Trust me, you will become silly after typing specialize so many times.... :D
Hence a good idea to name the specialized type instead of specializing on the fly ;)

Thaddy

  • Hero Member
  • *****
  • Posts: 14205
  • Probably until I exterminate Putin.
Re: Inheritance with generics
« Reply #6 on: July 03, 2017, 11:57:40 am »
Trust me, you will become silly after typing specialize so many times.... :D
Hence a good idea to name the specialized type instead of specializing on the fly ;)
You missed the point... When the hierarchy needs to  be generic, you need to use the syntax as per my example one. (This is also what OP wants)
If you name the specialized type (as per my remark later) you will loose a fully generic hierarchy... BIG difference.
Specialize a type, not a var.

 

TinyPortal © 2005-2018