Recent

Author Topic: Error: Generics without specialization cannot be used as a type for a variable  (Read 1007 times)

dmitryb

  • Jr. Member
  • **
  • Posts: 69
Hello

Why I am getting a compilation error?

Quote
Error: Generics without specialization cannot be used as a type for a variable

In Delphi, this file compiles without errors.


Code: Pascal  [Select][+][-]
  1. type
  2.   TBaseRowViewEh = class;
  3.   TBaseFieldViewEh = class;
  4.   TBaseTableLinkEh = class;
  5. ...
  6.  
  7.   TFieldViewListEh = class(TEnumerable<TBaseFieldViewEh>)
  8.   private
  9. ...



bytebites

  • Hero Member
  • *****
  • Posts: 672
Use {$mode delphi} or with {$mode objfpc} specialize 
Code: Pascal  [Select][+][-]
  1. TFieldViewListEh = class(specialize TEnumerable<TBaseFieldViewEh>)

paweld

  • Hero Member
  • *****
  • Posts: 1211
The ObjFPC mode (which is the default in Lazarus if the mode is not explicitly specified) requires specialization for generics.
So you must add the specialize keyword for each generic type ( https://www.freepascal.org/docs-html/ref/refse54.html ), or force the Delphi mode in the unit header, eg.
Code: Pascal  [Select][+][-]
  1. unit MyUnit;
  2. {$mode delphi}
  3. //....
  4.  
  5.  
  6.  
Best regards / Pozdrawiam
paweld

dmitryb

  • Jr. Member
  • **
  • Posts: 69
The documentation for {$mode delphi} does not contain information about compilation mode changes relative to the reserved word `specialized`.

https://www.freepascal.org/docs-html/3.2.0/prog/progse74.html

Is there a complete description of {$mode delphi}?

paweld

  • Hero Member
  • *****
  • Posts: 1211
because Delphi mode does not require specialization the spetialize keyword.
And the module you're showing doesn't have Delphi mode set, that's why you get an error about no specialization.
Modify this unit as follows and you should not have the error:
Code: Pascal  [Select][+][-]
  1. unit EhLib.TableLinks;
  2.  
  3. {$mode delphi}
  4.  
  5. interface
  6.  
  7. uses
  8.  
  9. //rest of code
  10.  
« Last Edit: August 14, 2024, 07:14:50 am by paweld »
Best regards / Pozdrawiam
paweld

PascalDragon

  • Hero Member
  • *****
  • Posts: 5672
  • Compiler Developer
Is there a complete description of {$mode delphi}?

The Delphi manuals.

because Delphi mode does not require specialization.

Please use the correct terms. Specialization is required no matter the mode and which is what [specialize] Identifier<Type[, Type[, …]]> does. What is not used in mode Delphi are the generic and specialize keywords.

paweld

  • Hero Member
  • *****
  • Posts: 1211
Quote from: PascalDragon
Please use the correct terms. Specialization is required no matter the mode and which is what [specialize] Identifier<Type[, Type[, …]]> does. What is not used in mode Delphi are the generic and specialize keywords.
Thank you for the clarification
Best regards / Pozdrawiam
paweld

 

TinyPortal © 2005-2018