Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2015-02

Generic: restricting a specialized type

3 messages · 2 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

silvioprog

Hello,

I'm trying to port a Delphi code to FPC, and it has a structure like:

  TType = class(TObject)
  end;
  TTest<T1; T2: TType> = class(TObject)
  end;
  TFoo = class(TType)
  end;
  TBar = class(TTest<string, TFoo>)
  end;

I.e., T1 accepts any type, but T2 accepts _only_ TType and/or their descendants.

I tried the following code in FPC:

  TType = class(TObject)
  end;
  generic TTest<T1; T2: TType> = class(TObject)
  end;
  TFoo = class(TType)
  end;
  TBar = class(specialize TTest<string, TFoo>)
  end;

But:

===
Compile Project, Target: project1.exe: Exit code 1, Errors: 2 Unit1.pas(30,34) Error: class type expected, but got "AnsiString" Unit1.pas(30,47) Error: class type expected, but got "<erroneous type>" ===

If I declare "T2" instead of "T2: TType", it compiles, but using this declaration, a programmer can use accidentally a non descendant type of TType.

Is there an issue about this in bugtracker?

Thank you!

Silvio Clécio My public projects - github.com/silvioprog

Reply by email

Sven Barth
to silvioprog
silvioprog wrote 46 lines
Hello, I'm trying to port a Delphi code to FPC, and it has a structure like: TType = class(TObject) end; TTest<T1; T2: TType> = class(TObject) end; TFoo = class(TType) end; TBar = class(TTest<string, TFoo>) end; I.e., T1 accepts any type, but T2 accepts _only_ TType and/or their descendants. I tried the following code in FPC: TType = class(TObject) end; generic TTest<T1; T2: TType> = class(TObject) end; TFoo = class(TType) end; TBar = class(specialize TTest<string, TFoo>) end; But: === Compile Project, Target: project1.exe: Exit code 1, Errors: 2 Unit1.pas(30,34) Error: class type expected, but got "AnsiString" Unit1.pas(30,47) Error: class type expected, but got "<erroneous type>" === If I declare "T2" instead of "T2: TType", it compiles, but using this declaration, a programmer can use accidentally a non descendant type of TType. Is there an issue about this in bugtracker?

Looks like a bug. Please report.

As a workaround you can move the constraint to the first type instead of the second and specialize with "<TFoo, string>".

Regards,
Sven

Reply by email

silvioprog
to Sven Barth
Sven Barth wrote 52 lines
On 08.02.2015 04:10, silvioprog wrote: > Hello, > > I'm trying to port a Delphi code to FPC, and it has a structure like: > > TType = class(TObject) > end; > > TTest<T1; T2: TType> = class(TObject) > end; > > TFoo = class(TType) > end; > > TBar = class(TTest<string, TFoo>) > end; > > I.e., T1 accepts any type, but T2 accepts _only_ TType and/or their > descendants. > > I tried the following code in FPC: > > TType = class(TObject) > end; > > generic TTest<T1; T2: TType> = class(TObject) > end; > > TFoo = class(TType) > end; > > TBar = class(specialize TTest<string, TFoo>) > end; > > But: > > === > Compile Project, Target: project1.exe: Exit code 1, Errors: 2 > Unit1.pas(30,34) Error: class type expected, but got "AnsiString" > Unit1.pas(30,47) Error: class type expected, but got "<erroneous type>" > === > > If I declare "T2" instead of "T2: TType", it compiles, but using this > declaration, a programmer can use accidentally a non descendant type of > TType. > > Is there an issue about this in bugtracker? > Looks like a bug. Please report.

Done:

http://bugs.freepascal.org/view.php?id=27424

As a workaround you can move the constraint to the first type instead of the second and specialize with "<TFoo, string>".

I'll try it.

Thank you! (y)

Regards,

Sven
Silvio Clécio My public projects - github.com/silvioprog

Reply by email