Recent

Author Topic: [SOLVED] Generics without specialization cannot be used as a type  (Read 5829 times)

yogawa

  • Newbie
  • Posts: 4
I almost crazy because of this bug, please somebody help me

I have declared a class in "type" sectioin like this
Code: [Select]
  TActivityData = class
  public
    duration: integer;
    date: string;
    Data: string;
  end;

and this is my code in "var" section
what is wrong with this code actually?
Code: [Select]
activities: TObjectQueue<TActivityData>;
and this is the error messages in IDE
Code: [Select]
main.pas(125,15) Error: Generics without specialization cannot be used as a type for a variable
main.pas(125,27) Error: Error in type definition
main.pas(125,27) Fatal: Syntax error, ";" expected but "<" found

« Last Edit: November 12, 2023, 05:48:38 pm by yogawa »

TRon

  • Hero Member
  • *****
  • Posts: 4377
Re: [ASK] Generics without specialization cannot be used as a type
« Reply #1 on: November 11, 2023, 04:59:21 am »
I almost crazy because of this bug, please somebody help me
It is not bug, it is a feature.

Quote
what is wrong with this code actually?
Probably not using mode Delphi ?

Interesting bits can be read here in the manual
Today is tomorrow's yesterday.

yogawa

  • Newbie
  • Posts: 4
Re: [ASK] Generics without specialization cannot be used as a type
« Reply #2 on: November 11, 2023, 04:48:00 pm »
I almost crazy because of this bug, please somebody help me
It is not bug, it is a feature.

Quote
what is wrong with this code actually?
Probably not using mode Delphi ?

Interesting bits can be read here in the manual

what do you mean of not using mode delphi?

TRon

  • Hero Member
  • *****
  • Posts: 4377
Re: [ASK] Generics without specialization cannot be used as a type
« Reply #3 on: November 11, 2023, 04:57:29 pm »
what do you mean of not using mode delphi?
You seem to declare your generics as Delphi would accept it. Such declarations do not work in mode objfpc as for that mode you need to specialize first.  But that was already explained in the link posted in my previous post.

See also compiler modes
Today is tomorrow's yesterday.

cdbc

  • Hero Member
  • *****
  • Posts: 2683
    • http://www.cdbc.dk
Re: [ASK] Generics without specialization cannot be used as a type
« Reply #4 on: November 11, 2023, 05:05:29 pm »
Hi
Code: Pascal  [Select][+][-]
  1. {$mode objfpc}{$H+}
  2. type
  3.   TActivities = specialize TObjectQueue<TActivityData>;
  4. ...
  5. var
  6.   activities: TActivities;
  7. ///// or
  8.   activities: specialize TObjectQueue<TActivityData>;
With the second var option, you must use the specialization throughout your code.
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE6/QT6 -> FPC Release -> Lazarus Release &  FPC Main -> Lazarus Main

yogawa

  • Newbie
  • Posts: 4
Re: [ASK] Generics without specialization cannot be used as a type
« Reply #5 on: November 12, 2023, 05:45:02 pm »
what do you mean of not using mode delphi?
You seem to declare your generics as Delphi would accept it. Such declarations do not work in mode objfpc as for that mode you need to specialize first.  But that was already explained in the link posted in my previous post.

See also compiler modes

I just know that lazarus has many modes  :D
Finally I can compile my code after I changed it to delphi mode

Thanks

In Addition:
In case someone else has the same problem please read this
https://www.freepascal.org/docs-html/prog/progap4.html#progse62.html

yogawa

  • Newbie
  • Posts: 4
Re: [ASK] Generics without specialization cannot be used as a type
« Reply #6 on: November 12, 2023, 05:48:13 pm »
Hi
Code: Pascal  [Select][+][-]
  1. {$mode objfpc}{$H+}
  2. type
  3.   TActivities = specialize TObjectQueue<TActivityData>;
  4. ...
  5. var
  6.   activities: TActivities;
  7. ///// or
  8.   activities: specialize TObjectQueue<TActivityData>;
With the second var option, you must use the specialization throughout your code.
Regards Benny

I just know that freepascal has this kind of syntax
Thank you

 

TinyPortal © 2005-2018