View Issue Details

IDProjectCategoryView StatusLast Update
0016588FPCCompilerpublic2019-07-18 21:11
ReporterJonas SchäferAssigned To 
PrioritynormalSeverityfeatureReproducibilityN/A
Status feedbackResolutionopen 
Platformx86_64OSlinuxOS Versionfedora 12
Product VersionProduct Build 
Target VersionFixed in Version 
Summary0016588: Generic declaration with unspecified ancestor
DescriptionHi all,

I just ran into a situation where I could use something like this:

type
  TCommonBaseClass = class;

  TSomeObjectTree1 = class(TCommonBaseClass);
  TSomeObjectTree2 = class(TCommonBaseClass);

  generic TGenericChain<TObjectClass> = class (TObjectClass)
  // ...
  end;

  TTree1Chain = specialize TGenericChain<TSomeObjectTree1>;
  TTree2Chain = specialize TGenericChain<TSomeObjectTree2>;


What this all is about, I have two object trees which are very similar. These chains contain multiple of these objects, but need to inherit from their baseclass too.
As the functionallity of the chain is exactly the same it would be nice to have the possibility to have these generic inheritance.

Further information will be kindly provided if needed.
Tagsgenerics
Fixed in Revision
FPCOldBugId
FPCTarget
Attached Files

Relationships

has duplicate 0019549 closedSven Barth Generic class can't derive from parameterised type 
related to 0032054 new bug when using generics with the generic parameter as descendant 

Activities

Paul Ishenin

2010-12-01 09:49

developer   ~0043833

Please attach a full example.

Jonas Schäfer

2011-01-11 19:47

reporter   ~0045111

I hope that this is okay. It wont compile of course.

2011-01-11 19:47

 

genin.tar.gz (10,240 bytes)

Paul Ishenin

2011-01-13 13:06

developer   ~0045141

Does any other language supports such constructions? At least delphi does not.

Alexander Shishkin

2011-01-13 14:22

reporter   ~0045148

Last edited: 2011-01-13 15:31

C++ supports it AFAIK

java does not support it ("Cannot refer to the type parameter T as a supertype")

C# does not support it http://msdn.microsoft.com/en-us/library/f47cb6h7%28v=VS.90%29.aspx

http://msdn.microsoft.com/en-us/library/c6cyy67b.aspx
"C# does not allow the type parameter to be used as the base class for the generic type."
So it one of differences between template and generic applying to C++ and C#.

seems to me C++ is the only language supporting such constructions.

Ian Hickson

2012-07-03 22:37

reporter   ~0060838

Compilable test case at: http://junkyard.damowmow.com/469

Ian Hickson

2014-01-26 17:26

reporter   ~0072697

Right now I'm working around this by just using macros instead. It's not as pretty, but does the same thing. Given that that works, it's not clear why we wouldn't want to allow it in generics.

Ian Hickson

2015-03-17 05:05

reporter   ~0082028

Dart also supports this (indirectly, via "with").

I think it's interesting that on trunk, this works:

type
   TRoot = class abstract procedure Foo(); end;
   TA = class (TRoot) procedure Foo(); end;
   generic TMixin <T: TRoot> = class(T) procedure Foo(); end;
   TAMixin = class(specialize TMixin<TA>) end;

...but this doesn't (making Foo virtual, now in TMixin "There is no method in an ancestor class to be overridden"):

type
   TRoot = class abstract procedure Foo(); virtual; end;
   TA = class (TRoot) procedure Foo(); override; end;
   generic TMixin <T: TRoot> = class(T) procedure Foo(); override; end;
   TAMixin = class(specialize TMixin<TA>) end;

Given that TMixin knows that T will be a subclass of TRoot, IMHO this should ideally work. Not sure how this would interact with "reintroduce" in a subclass of TRoot used as the specialized type argument, admittedly.

Thaddy de Koning

2018-05-20 11:13

reporter   ~0108444

Last edited: 2018-05-20 11:15

View 2 revisions

C++ does not support it, It supports it with constraints.
Type inference from an abstract without type constraint is not supported.
In this case T can not be resolved to a class constraint because T is AnyType.
You can not derive from anything if you are not sure it is a class.

Issue History

Date Modified Username Field Change
2010-05-28 16:29 Jonas Schäfer New Issue
2010-12-01 09:49 Paul Ishenin Note Added: 0043833
2010-12-01 09:49 Paul Ishenin Status new => feedback
2010-12-01 09:49 Paul Ishenin Tag Attached: generics
2011-01-11 19:47 Jonas Schäfer Note Added: 0045111
2011-01-11 19:47 Jonas Schäfer File Added: genin.tar.gz
2011-01-13 13:06 Paul Ishenin Note Added: 0045141
2011-01-13 14:22 Alexander Shishkin Note Added: 0045148
2011-01-13 14:26 Alexander Shishkin Note Edited: 0045148
2011-01-13 14:34 Alexander Shishkin Note Edited: 0045148
2011-01-13 14:45 Alexander Shishkin Note Edited: 0045148
2011-01-13 14:47 Alexander Shishkin Note Edited: 0045148
2011-01-13 14:53 Alexander Shishkin Note Edited: 0045148
2011-01-13 14:57 Alexander Shishkin Note Edited: 0045148
2011-01-13 15:06 Alexander Shishkin Note Edited: 0045148
2011-01-13 15:06 Alexander Shishkin Note Edited: 0045148
2011-01-13 15:31 Alexander Shishkin Note Edited: 0045148
2012-06-14 17:38 Sven Barth Relationship added has duplicate 0019549
2012-07-03 22:37 Ian Hickson Note Added: 0060838
2014-01-26 17:26 Ian Hickson Note Added: 0072697
2015-03-17 05:05 Ian Hickson Note Added: 0082028
2018-05-18 16:06 Sven Barth Relationship added has duplicate 0032054
2018-05-18 16:08 Sven Barth Relationship deleted has duplicate 0032054
2018-05-18 16:08 Sven Barth Relationship added related to 0032054
2018-05-20 11:13 Thaddy de Koning Note Added: 0108444
2018-05-20 11:15 Thaddy de Koning Note Edited: 0108444 View Revisions