Recent

Author Topic: Problem with generics  (Read 3134 times)

cappe

  • Full Member
  • ***
  • Posts: 192
Problem with generics
« on: June 29, 2017, 11:39:56 am »
I would like to do this

type
  TNotifySR = procedure (Dt: String) of Object;

  TSList = specialize TList<TNotifySR>;     // <----  Error   :'(

How can correctly deifne TSList ?  ::)


Thaddy

  • Hero Member
  • *****
  • Posts: 16520
  • Kallstadt seems a good place to evict Trump to.
Re: Problem with generics
« Reply #1 on: June 29, 2017, 11:52:46 am »
I would like to do this

type
  TNotifySR = procedure (Dt: String) of Object;

  TSList = specialize TList<TNotifySR>;     // <----  Error   :'(

How can correctly deifne TSList ?  ::)
TSlist is already defined somewhere. It is the basic SList of one of the generics solutions. I think it is better to choose a different name!

But anyway, your code simply compiles in trunk:
Code: Pascal  [Select][+][-]
  1. program untitled;
  2. {$mode objfpc}
  3. uses generics.collections;
  4. type
  5.   TNotifySR = procedure (Dt: String) of Object;
  6.   TSList = specialize TList<TNotifySR>;     // <----  No Error in trunk  ;)
  7.  
  8. begin
  9. end.

If you do not have trunk, this is an example using fgl and 3.0.2:
Code: Pascal  [Select][+][-]
  1. program untitled;
  2. {$mode objfpc}
  3. uses fgl;
  4. type
  5.   TNotifySR = procedure (Dt: String) of Object;
  6.   TSList = specialize TFPGList<TNotifySR>;     // <----  No Error in 3.0.2  ;)
  7.  
  8. begin
  9. end.


« Last Edit: June 29, 2017, 12:20:59 pm by Thaddy »
But I am sure they don't want the Trumps back...

cappe

  • Full Member
  • ***
  • Posts: 192
Re: Problem with generics
« Reply #2 on: June 29, 2017, 12:15:15 pm »
I use FPC 2.6.4 and I have to use this version for certain reasons.

TSsList = specialize TList<TNotifySR>;
Give me the error:
Error: Specialization is only supported for generic types

Pascal

  • Hero Member
  • *****
  • Posts: 932
Re: Problem with generics
« Reply #3 on: June 29, 2017, 12:20:34 pm »
Use TFPGList of unit fgl:

Code: Pascal  [Select][+][-]
  1. TSsList = specialize TFPGList<TNotifySR>;
laz trunk x64 - fpc trunk i386 (cross x64) - Windows 10 Pro x64 (21H2)

Thaddy

  • Hero Member
  • *****
  • Posts: 16520
  • Kallstadt seems a good place to evict Trump to.
Re: Problem with generics
« Reply #4 on: June 29, 2017, 12:22:45 pm »
Pascal, that's what I showed him already. Indeed that should work in 2.6.4.
Cappe, you really need to upgrade. I can't even test anymore with 2.6.4 and it is officially unsupported.
And you can't use TList from classes as a generic list. In 2.6.4 you would use TFPGList from fgl.

You probably have seen an example for later versions? Well, upgrade.
« Last Edit: June 29, 2017, 12:27:04 pm by Thaddy »
But I am sure they don't want the Trumps back...

cappe

  • Full Member
  • ***
  • Posts: 192
Re: Problem with generics
« Reply #5 on: June 29, 2017, 12:34:14 pm »
75/5000
In fact, I've just tried your second example and it works.
Thank you
 8-)

Pascal

  • Hero Member
  • *****
  • Posts: 932
Re: Problem with generics
« Reply #6 on: June 29, 2017, 12:51:57 pm »
Pascal, that's what I showed him already. Indeed that should work in 2.6.4.
Sorry, i was to quick in reading your sample  %)
laz trunk x64 - fpc trunk i386 (cross x64) - Windows 10 Pro x64 (21H2)

 

TinyPortal © 2005-2018