Recent

Author Topic: [SOLVED] Generics  (Read 3589 times)

egsuh

  • Hero Member
  • *****
  • Posts: 1752
[SOLVED] Generics
« on: September 20, 2017, 08:13:15 am »
Hi,

Regarding generics, is there any change between fpc 3.0.0 and fpc 3.0.2?

I wrote a unit using "specialize" of TFPCObjectList  as follows:

   TCF = specialize TFPCObjectList<TMyClass>;


This compiles well on a machine with FPC 3.0.0, but makes following error on machine with FPC 3.0.0.

   codeframe.pas(55,19) Error: Identifier not found "specialize"

What would be possible reason?

Regards,
« Last Edit: September 20, 2017, 08:21:16 am by egsuh »

egsuh

  • Hero Member
  • *****
  • Posts: 1752
Re: Generics
« Reply #1 on: September 20, 2017, 08:20:56 am »
I found the reason.

This works only in  {$mode objfpc}, not in {$mode delphi}.

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: [SOLVED] Generics
« Reply #2 on: September 20, 2017, 08:33:07 am »
This works only in  {$mode objfpc}, not in {$mode delphi}.
Yes and no.

Yes, your specific declaration indeed does, but you can use generics in both modes. It just works a bit differently when declaring generics (or specialization as in your example).

If you want to know the difference then have a look in these chapters and for your case i guess read the Delphi specific parts to see how to 'specialize' a generic in Delphi mode.

Thaddy

  • Hero Member
  • *****
  • Posts: 18764
  • To Europe: simply sell USA bonds: dollar collapses
Re: [SOLVED] Generics
« Reply #3 on: September 20, 2017, 08:44:56 am »
basically mode delphi is a more concise syntax (drop specialize). It is my impression that the compiler people actually working on generics prefer mode delphi themselves.
Also note the mode is per unit: you can use a unit that uses objfpc generics syntax in another unit using mode delphi. It is transparent.
If Europe sells their USA bonds the USD will collapse. Europe can affort that given average state debts. The USA can't affort that. Just an advice...

egsuh

  • Hero Member
  • *****
  • Posts: 1752
Re: [SOLVED] Generics
« Reply #4 on: September 20, 2017, 10:13:43 am »
Molly and Thaddy,

Thank you very much for your replies. I was just about to ask whether a project may be mixed with both modes, and you have already given the answer.

PascalDragon

  • Hero Member
  • *****
  • Posts: 6349
  • Compiler Developer
Re: [SOLVED] Generics
« Reply #5 on: September 29, 2017, 10:20:11 pm »
It is my impression that the compiler people actually working on generics prefer mode delphi themselves.

Considering that I'm the only one that is working on generics I can definitely say that I prefer FPC's syntax, cause the Delphi one is a PITA to parse. To see what I mean, just keep in mind that the non Delphi modes allow this already while mode Delphi does not:

Code: Pascal  [Select][+][-]
  1. someint := specialize SomeFunc<Integer>(SomeArg) + specialize SomeOtherFunc<Integer>(SomeOtherArg);

Mode Delphi can not yet handle such "complex" expressions, there you'd need to write this:

Code: Pascal  [Select][+][-]
  1. tmp := SomeFunc<Integer>(SomeArg);
  2. someint := tmp + SomeOtherFunc<Integer>(SomeOtherArg);

 

TinyPortal © 2005-2018