Recent

Author Topic: "Error: Illegal Expression" with generics  (Read 2132 times)

deadserious

  • New Member
  • *
  • Posts: 25
"Error: Illegal Expression" with generics
« on: May 13, 2024, 04:17:54 am »
I'm trying to get my cocinasync library to compile under freepascal trunk  (thanks to anonymous methods) and running into a problem instantiating a generic type.  Compile is failing on the following code in DELPHIUNICODE mode:

Code: Pascal  [Select][+][-]
  1. class constructor TDefaultJob<T>.Create;
  2. begin
  3.   FPool := TStack<IJob<T>>.Create;
  4. end;
  5.  

The code compiles in Delphi just fine and can be found in the cocinasync.jobs unit.

BildatBoffin

  • New Member
  • *
  • Posts: 30
Re: "Error: Illegal Expression" with generics
« Reply #1 on: May 13, 2024, 11:57:49 am »
just to be sure, mode delphi toogled on ?

deadserious

  • New Member
  • *
  • Posts: 25
Re: "Error: Illegal Expression" with generics
« Reply #2 on: May 13, 2024, 01:34:02 pm »
Yes, as stated in the original message it's in DELPHIUNICODE mode.

deadserious

  • New Member
  • *
  • Posts: 25
Re: "Error: Illegal Expression" with generics
« Reply #3 on: May 13, 2024, 01:45:24 pm »
Incidentally, I can get around the error by declaring a type alias as such...

Code: Pascal  [Select][+][-]
  1.   TJobStack<T> = class(TStack<IJob<T>>)  
  2.   end;
  3.  

Thaddy

  • Hero Member
  • *****
  • Posts: 16201
  • Censorship about opinions does not belong here.
Re: "Error: Illegal Expression" with generics
« Reply #4 on: May 13, 2024, 02:00:38 pm »
And fpc trunk/main a.k.a. 3.3.1 or 3.2.2/3.2.3 ?
I need more code to reproduce.
If I smell bad code it usually is bad code and that includes my own code.

deadserious

  • New Member
  • *
  • Posts: 25
Re: "Error: Illegal Expression" with generics
« Reply #5 on: May 14, 2024, 10:18:45 pm »
Trunk/main since my library uses anonymous methods.

Thaddy

  • Hero Member
  • *****
  • Posts: 16201
  • Censorship about opinions does not belong here.
Re: "Error: Illegal Expression" with generics
« Reply #6 on: May 14, 2024, 10:37:20 pm »
Code: Pascal  [Select][+][-]
  1. //Both modeswitches in place?
  2. {$modeswitch functionreferences}{$modeswitch anonymousfunctions}
If I smell bad code it usually is bad code and that includes my own code.

PascalDragon

  • Hero Member
  • *****
  • Posts: 5769
  • Compiler Developer
Re: "Error: Illegal Expression" with generics
« Reply #7 on: May 14, 2024, 10:59:02 pm »
Code: Pascal  [Select][+][-]
  1. class constructor TDefaultJob<T>.Create;
  2. begin
  3.   FPool := TStack<IJob<T>>.Create;
  4. end;
  5.  

FPC does not support nested inline specializations in mode Delphi yet. You should be able to do something like this which should also work in Delphi (not tested):

Code: Pascal  [Select][+][-]
  1. class constructor TDefaultJob<T>.Create;
  2. type
  3.   IJobT = IJob<T>;
  4. begin
  5.   FPool := TStack<IJobT>.Create;
  6. end;
  7.  

deadserious

  • New Member
  • *
  • Posts: 25
Re: "Error: Illegal Expression" with generics
« Reply #8 on: May 14, 2024, 11:14:41 pm »
Code: Pascal  [Select][+][-]
  1. class constructor TDefaultJob<T>.Create;
  2. begin
  3.   FPool := TStack<IJob<T>>.Create;
  4. end;
  5.  

FPC does not support nested inline specializations in mode Delphi yet. You should be able to do something like this which should also work in Delphi (not tested):

Code: Pascal  [Select][+][-]
  1. class constructor TDefaultJob<T>.Create;
  2. type
  3.   IJobT = IJob<T>;
  4. begin
  5.   FPool := TStack<IJobT>.Create;
  6. end;
  7.  

If I wanted to try to fix this in the parser, do you have a hint as to where I should start looking?

PascalDragon

  • Hero Member
  • *****
  • Posts: 5769
  • Compiler Developer
Re: "Error: Illegal Expression" with generics
« Reply #9 on: May 22, 2024, 10:24:36 pm »
If I wanted to try to fix this in the parser, do you have a hint as to where I should start looking?

Don't. Just don't.

 

TinyPortal © 2005-2018