[fpc-pascal] Generic: restricting a specialized type

silvioprog silvioprog at gmail.com
Sun Feb 8 04:10:38 CET 2015


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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-pascal/attachments/20150208/8b366bf3/attachment.html>


More information about the fpc-pascal mailing list