View Issue Details

IDProjectCategoryView StatusLast Update
0035943FPCCompilerpublic2019-08-19 16:58
ReportersilvioprogAssigned To 
PrioritynormalSeverityminorReproducibilityalways
Status newResolutionopen 
Product Version3.3.1Product Build 
Target VersionFixed in Version 
Summary0035943: Generics: Error: Class type expected, but got "T"
DescriptionHi.

Consider the following test:

program Project1;

{$MODE DELPHI}

uses Classes;

type
  TFoo<T: TPersistent> = class(TPersistent)
  end;

  TBar = class(TPersistent)
  public
    function Test<T: TPersistent>: TFoo<T>;
  end;

function TBar.Test<T>: TFoo<T>;
begin
  Result := nil;
end;

begin
end.


On Delphi, it compiles fine, however, on FPC, I've got the following problem:

Compile Project, Target: project1: Exit code 1, Errors: 3
project1.lpr(16,29) Error: Class type expected, but got "T"
project1.lpr(16,15) Error: Function header doesn't match any method of this class "Test$1:<erroneous type>;"
project1.lpr(13,14) Error: Found declaration: Test$1:TFoo$1<PROJECT1.TBar.Test$1T>;


Is this a bug, or some special switch missing in the example?

Thank you!
Tagsgenerics
Fixed in Revision
FPCOldBugId
FPCTarget
Attached Files

Relationships

related to 0031437 new Method using generics constraint inside class cause compile error 

Activities

Thaddy de Koning

2019-08-15 08:29

reporter   ~0117680

Last edited: 2019-08-15 08:33

View 3 revisions

A rewrite compiles;
program Project1;

{$MODE DELPHI}

uses Classes;

type
  TFoo<T: TPersistent> = class(TPersistent)
  end;

  TBar<T:Tpersistent> = class(TPersistent)
  public
    function Test: TFoo<T>;
  end;

function TBar<T>.Test: TFoo<T>;
begin
  Result := nil;
end;
begin
end.


silvioprog

2019-08-16 15:26

reporter   ~0117702

Please don't try to obfuscate my issue with a non sense answer. You are suggesting a class specialization, but the issue refers to a bug in *method specialization*.

Thaddy de Koning

2019-08-17 14:15

reporter   ~0117716

It is not obfuscating it is offering an alternative syntax that works both in Delphi and Freepascal.

Akira1364

2019-08-19 04:55

reporter   ~0117734

Last edited: 2019-08-19 16:34

View 7 revisions

> It is not obfuscating it is offering an alternative syntax that works both in Delphi and Freepascal.

Doesn't solve the issue, though, which seems to be another instance of the compiler caring about what type "T" is long before it should, as opposed to basically ignoring it completely until specialization time.

To be clear, AFAIK, FPC's generics are not even *supposed* to be restricted to the same extent Delphi's are, so in theory they allow for a lot more. That fact just kind of gets obfuscated sometimes by parser bugs / other compiler bugs.

Delphi generics, generally speaking, behave exactly like C# generics, while FPC generics seem to be clearly modeled after C++.

Which is a good thing IMO, as it's the reason why you *can* for example overload arithmetic operators for generics in FPC (whereas Delphi very annoyingly doesn't allow that.)

Thaddy de Koning

2019-08-19 08:04

reporter   ~0117735

I agree with that. Although this specific case is about Delphi compatibility. Indeed, decoration the class itself as generic is currently a necessity if it contains generic methods.

Issue History

Date Modified Username Field Change
2019-08-09 17:14 silvioprog New Issue
2019-08-10 13:38 Sven Barth Tag Attached: generics
2019-08-10 13:41 Sven Barth Relationship added related to 0031437
2019-08-15 08:29 Thaddy de Koning Note Added: 0117680
2019-08-15 08:31 Thaddy de Koning Note Edited: 0117680 View Revisions
2019-08-15 08:33 Thaddy de Koning Note Edited: 0117680 View Revisions
2019-08-16 15:26 silvioprog Note Added: 0117702
2019-08-17 14:15 Thaddy de Koning Note Added: 0117716
2019-08-19 04:55 Akira1364 Note Added: 0117734
2019-08-19 04:55 Akira1364 Note Edited: 0117734 View Revisions
2019-08-19 04:56 Akira1364 Note Edited: 0117734 View Revisions
2019-08-19 04:58 Akira1364 Note Edited: 0117734 View Revisions
2019-08-19 05:00 Akira1364 Note Edited: 0117734 View Revisions
2019-08-19 08:04 Thaddy de Koning Note Added: 0117735
2019-08-19 16:33 Akira1364 Note Edited: 0117734 View Revisions
2019-08-19 16:34 Akira1364 Note Edited: 0117734 View Revisions