View Issue Details

IDProjectCategoryView StatusLast Update
0025678FPCCompilerpublic2020-07-04 10:52
ReporterConstantine Yannakopoulos Assigned To 
PrioritynormalSeverityminorReproducibilityalways
Status newResolutionopen 
PlatformWin32OSWindows 
Product Version2.7.1 
Summary0025678: Nested generic parameter types inside constraints compile in Delphi XE+ but not in FPC
DescriptionThe following construct:

type
  TConstraintClass<T> = class
  end;

  TGenericClass<T; U: TConstraintClass<T>> = class
  end;

compiles in Delphi XE or later but in FPC it produces the error "Identifier not found 'T'" at the T generic parameter declaration in the constraint for U.

This construct allows the implementation of the Strategy/Policy pattern using generics (see attached example) so it would be nice if it worked in FPC.
Steps To ReproduceTry to compile the attached example.
Additional InformationThe attached example is a simplified case where this construct could be used in "real" code that actually does something. If it is too complicated please tell me to upload a simpler one.
Tagsgenerics
Fixed in Revision
FPCOldBugId
FPCTarget
Attached Files

Relationships

has duplicate 0037017 resolvedSven Barth Generics 

Activities

Constantine Yannakopoulos

2014-02-05 12:04

reporter  

GenericConstraints.dpr (5,233 bytes)

Thaddy de Koning

2017-02-22 06:53

reporter   ~0098365

Last edited: 2017-02-22 06:58

View 2 revisions

Are you serious on reporting a bug against 2.7.1?
Try at least 3.0.2 or the real trunk which is 3.1.1.

To help you I did try:
Yes, does not compile yet, even in 3.1.1-r35464. I also remember that this is a duplicate and devs are aware of it.

delfion

2017-02-22 08:00

reporter   ~0098366

Submitted 3 years ago.

Sven Barth

2017-03-03 17:36

manager   ~0098602

@Thaddy: please pay attention to the dates. Someone has merely started monitoring this bug.

BlackShark

2020-05-06 19:53

reporter   ~0122648

Last edited: 2020-07-04 10:52

View 4 revisions

@Sven Barth
In my issue (https://bugs.freepascal.org/view.php?id=37017) you advised me to use the next:
"As a workaround you can do it like this:

type
  TItemsFactoryTemplate<T> = class
  public type
    THolder = TItemHolderTemplate<T>;
  public
    function CreateItemHolder(const Item: T): THolder;
  end;"

This means that you don't understand essence of the problem :(((

Again, please.

TItemHolderTemplate<T> = class
private
  FData: T;
public
  constructor Create(const AData: T); virtual;
  property Data read FData;
end;

TItemsFactoryTemplate<T> = class
 public type
    THolder = TItemHolderTemplate<T>;
 public
    function CreateItemHolder(const Item: T): THolder;
end;

// THE CLASS IS DESCENDANT OF TItemHolderTemplate<T>
TItemHolderString = class(TItemHolderTemplate<string>)
private
  FNewPropery: int32;
public
  property NewProperty: int32 read FNewPropery write FNewPropery;
end;

// specialization for using
TItemsFactoryString = class(TItemsFactoryTemplate<string>); <--- THIS FACTORY WILL NEVER CREATE TItemHolderString, IT WILL CREATE TItemHolderTemplate<string> ONLY

If would be possible to define factory as:

TItemsFactoryTemplate<T; H: TItemHolderTemplate<T>>
public
    function CreateItemHolder(const Item: T): H;
...
end;
...
implementation

function TItemsFactoryTemplate<T; H>.CreateItemHolder(const Item: T): H;
begin
  Result := H.Create(Item); <-- HERE MUST BE CREATE ANY DESCENDANTS OF TItemHolderTemplate<T> NOT ONLY HIM DIRECT SPECIALIZATIONS
end;

// it is specialization for using, where I want to define a class of factory from the template for creating TItemHolderString
TItemsFactoryString = class(TItemsFactoryTemplate<string, TItemHolderString>);

Issue History

Date Modified Username Field Change
2014-02-05 12:04 Constantine Yannakopoulos New Issue
2014-02-05 12:04 Constantine Yannakopoulos File Added: GenericConstraints.dpr
2014-02-05 12:19 Maciej Izak Tag Attached: generics
2017-02-22 06:53 Thaddy de Koning Note Added: 0098365
2017-02-22 06:58 Thaddy de Koning Note Edited: 0098365 View Revisions
2017-02-22 08:00 delfion Note Added: 0098366
2017-03-03 17:36 Sven Barth Note Added: 0098602
2020-05-06 15:11 Sven Barth Relationship added has duplicate 0037017
2020-05-06 19:53 BlackShark Note Added: 0122648
2020-05-06 19:58 BlackShark Note Edited: 0122648 View Revisions
2020-07-04 10:51 BlackShark Note Edited: 0122648 View Revisions
2020-07-04 10:52 BlackShark Note Edited: 0122648 View Revisions