Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2008-03

Generics

14 messages · 5 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Damien Gerard

I would like to make a generic class, like this :

{$mode objfpc}
generic TMCollection<_T> = class(TObject)
private
     FDefaultItem: _T;  // line 35
public
     constructor Create;
     destructor Destroy;override;
     [...]
end;
But I have got the following :
commons.pas(35,21) Error: Identifier not found "_T"
commons.pas(35,21) Error: Error in type definition
commons.pas(39,34) Error: Identifier not found "_T"
commons.pas(44,84) Error: Identifier not found "_T"

According to the documentation, I should use `var private`, but I have got :

Compiling ./commons.pas
commons.pas(33,3) Error: VAR and TYPE are allowed only in generics
commons.pas(35,21) Error: Identifier not found "_T"

Could someone tell me where I am wrong ?
Thanks !

Damien Gerard "Intelligence is 10 million rules." -- Douglas Lenat
ik
to Damien Gerard

After you define the generic you must first create a type for it:

type
TMyMcollection = specialize TMCollection<String>;

And use the TMyMcollection.
It seems like hard work, but it is much better then the way it look on a bad C++ and Java code :)

Ido

Damien Gerard wrote 48 lines
I would like to make a generic class, like this : {$mode objfpc} generic TMCollection<_T> = class(TObject) private FDefaultItem: _T; // line 35 public constructor Create; destructor Destroy;override; [...] end; But I have got the following : commons.pas(35,21) Error: Identifier not found "_T" commons.pas(35,21) Error: Error in type definition commons.pas(39,34) Error: Identifier not found "_T" commons.pas(44,84) Error: Identifier not found "_T" According to the documentation, I should use `var private`, but I have got : Compiling ./commons.pas commons.pas(33,3) Error: VAR and TYPE are allowed only in generics commons.pas(35,21) Error: Identifier not found "_T" Could someone tell me where I am wrong ? Thanks ! -- Damien Gerard "Intelligence is 10 million rules." -- Douglas Lenat _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Damien Gerard
to ik
ik wrote 5 lines
After you define the generic you must first create a type for it: type TMyMcollection = specialize TMCollection<String>;

Sorry but it did not change anything.
Additionally, I would prefer to define the specialization in another unit.

And use the TMyMcollection. It seems like hard work, but it is much better then the way it look on a bad C++ and Java code :)

:)

Show quoted text 61 lines
Ido On Mon, Mar 31, 2008 at 4:00 PM, Damien Gerard wrote: > > I would like to make a generic class, like this : > > {$mode objfpc} > > generic TMCollection<_T> = class(TObject) > private > FDefaultItem: _T; // line 35 > public > constructor Create; > destructor Destroy;override; > [...] > end; > > > But I have got the following : > commons.pas(35,21) Error: Identifier not found "_T" > commons.pas(35,21) Error: Error in type definition > commons.pas(39,34) Error: Identifier not found "_T" > commons.pas(44,84) Error: Identifier not found "_T" > > According to the documentation, I should use `var private`, but I > have > got : > > Compiling ./commons.pas > commons.pas(33,3) Error: VAR and TYPE are allowed only in generics > commons.pas(35,21) Error: Identifier not found "_T" > > Could someone tell me where I am wrong ? > Thanks ! > > > > -- > Damien Gerard > > > "Intelligence is 10 million rules." > -- Douglas Lenat > > > > > > _______________________________________________ > fpc-pascal maillist - > http://lists.freepascal.org/mailman/listinfo/fpc-pascal > -- http://ik.homelinux.org/ _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Damien Gerard "Intelligence is 10 million rules." -- Douglas Lenat
Damien Gerard
to Damien Gerard: I would like to make a generic class, like this : {$mode objfpc} generic TMCollection<_T>…
Damien Gerard wrote 31 lines
I would like to make a generic class, like this : {$mode objfpc} generic TMCollection<_T> = class(TObject) private FDefaultItem: _T; // line 35 public constructor Create; destructor Destroy;override; [...] end; But I have got the following : commons.pas(35,21) Error: Identifier not found "_T" commons.pas(35,21) Error: Error in type definition commons.pas(39,34) Error: Identifier not found "_T" commons.pas(44,84) Error: Identifier not found "_T" According to the documentation, I should use `var private`, but I have got : Compiling ./commons.pas commons.pas(33,3) Error: VAR and TYPE are allowed only in generics commons.pas(35,21) Error: Identifier not found "_T" Could someone tell me where I am wrong ? Thanks !

I forgot : fpc 2.2.0 and fpc 2.2.1. May be I should upgrade to 2.3.x ?

Damien Gerard "Intelligence is 10 million rules." -- Douglas Lenat
ik
to Damien Gerard

Can you create a small proof of concept, and attach it here ?

Ido

Damien Gerard wrote 57 lines
Le Mar 31, 2008 à 3:00 PM, Damien Gerard a écrit : > > I would like to make a generic class, like this : > > {$mode objfpc} > > generic TMCollection<_T> = class(TObject) > private > FDefaultItem: _T; // line 35 > public > constructor Create; > destructor Destroy;override; > [...] > end; > > > But I have got the following : > commons.pas(35,21) Error: Identifier not found "_T" > commons.pas(35,21) Error: Error in type definition > commons.pas(39,34) Error: Identifier not found "_T" > commons.pas(44,84) Error: Identifier not found "_T" > > According to the documentation, I should use `var private`, but I > have got : > > Compiling ./commons.pas > commons.pas(33,3) Error: VAR and TYPE are allowed only in generics > commons.pas(35,21) Error: Identifier not found "_T" > > Could someone tell me where I am wrong ? > Thanks ! > I forgot : fpc 2.2.0 and fpc 2.2.1. May be I should upgrade to 2.3.x ? -- Damien Gerard "Intelligence is 10 million rules." -- Douglas Lenat _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Damien Gerard
to ik
ik wrote
Can you create a small proof of concept, and attach it here ?

Here you are.
The unit is in development so it may remain some errors (I would like to convert another units with the use of generics).

BTW, is there a way to specifiy the template parameter must be a descendant of another class ?

Show quoted text 43 lines
Ido On Mon, Mar 31, 2008 at 4:27 PM, Damien Gerard wrote: > > Le Mar 31, 2008 à 3:00 PM, Damien Gerard a écrit : > >> >> I would like to make a generic class, like this : >> >> {$mode objfpc} >> >> generic TMCollection<_T> = class(TObject) >> private >> FDefaultItem: _T; // line 35 >> public >> constructor Create; >> destructor Destroy;override; >> [...] >> end; >> >> >> But I have got the following : >> commons.pas(35,21) Error: Identifier not found "_T" >> commons.pas(35,21) Error: Error in type definition >> commons.pas(39,34) Error: Identifier not found "_T" >> commons.pas(44,84) Error: Identifier not found "_T" >> >> According to the documentation, I should use `var private`, but I >> have got : >> >> Compiling ./commons.pas >> commons.pas(33,3) Error: VAR and TYPE are allowed only in generics >> commons.pas(35,21) Error: Identifier not found "_T" >> >> Could someone tell me where I am wrong ? >> Thanks ! >> > > > > I forgot : fpc 2.2.0 and fpc 2.2.1. May be I should upgrade to > 2.3.x ?
Damien Gerard "Intelligence is 10 million rules." -- Douglas Lenat
Peter Vreman
to Damien Gerard: Here you are. The unit is in development so it may remain some errors (I would like to…
Show quoted text 28 lines
>>> I would like to make a generic class, like this : >>> >>> {$mode objfpc} >>> >>> generic TMCollection<_T> = class(TObject) >>> private >>> FDefaultItem: _T; // line 35 >>> public >>> constructor Create; >>> destructor Destroy;override; >>> [...] >>> end; >>> >>> >>> But I have got the following : >>> commons.pas(35,21) Error: Identifier not found "_T" >>> commons.pas(35,21) Error: Error in type definition >>> commons.pas(39,34) Error: Identifier not found "_T" >>> commons.pas(44,84) Error: Identifier not found "_T" >>> >>> According to the documentation, I should use `var private`, but I >>> have got : >>> >>> Compiling ./commons.pas >>> commons.pas(33,3) Error: VAR and TYPE are allowed only in generics >>> commons.pas(35,21) Error: Identifier not found "_T" >>> >>> Could someone tell me where I am wrong ?

The forward declaration of TMCollection makes it a normal class and not a generic class.

Peter

Damien Gerard
to Peter Vreman
Peter Vreman wrote 32 lines
>>>> I would like to make a generic class, like this : >>>> >>>> {$mode objfpc} >>>> >>>> generic TMCollection<_T> = class(TObject) >>>> private >>>> FDefaultItem: _T; // line 35 >>>> public >>>> constructor Create; >>>> destructor Destroy;override; >>>> [...] >>>> end; >>>> >>>> >>>> But I have got the following : >>>> commons.pas(35,21) Error: Identifier not found "_T" >>>> commons.pas(35,21) Error: Error in type definition >>>> commons.pas(39,34) Error: Identifier not found "_T" >>>> commons.pas(44,84) Error: Identifier not found "_T" >>>> >>>> According to the documentation, I should use `var private`, but I >>>> have got : >>>> >>>> Compiling ./commons.pas >>>> commons.pas(33,3) Error: VAR and TYPE are allowed only in generics >>>> commons.pas(35,21) Error: Identifier not found "_T" >>>> >>>> Could someone tell me where I am wrong ? The forward declaration of TMCollection makes it a normal class and not a generic class.

Oki I understand.
I tried to use an Interface but I had a nice Segementation fault :)

Should I report it as a bug in the mantis ?

Damien Gerard "Intelligence is 10 million rules." -- Douglas Lenat
Damien Gerard
to Damien Gerard
Show quoted text 7 lines
Oki I understand. I tried to use an Interface but I had a nice Segementation fault :) Should I report it as a bug in the mantis ? <foo.pas>

Actually there is the same error with an intermediate class :(

Damien Gerard "Intelligence is 10 million rules." -- Douglas Lenat
Damien Gerard
to Micha Nelissen
Micha Nelissen wrote 11 lines
Damien Gerard wrote: >> I tried to use an Interface but I had a nice Segementation fault :) >> >> Should I report it as a bug in the mantis ? >> >> <foo.pas> > > Actually there is the same error with an intermediate class :( Segmentation faults in the compiler itself can/should always be reported. Thanks.

Reported as Issue #11075.

Damien Gerard "Intelligence is 10 million rules." -- Douglas Lenat
Damien Gerard
to Damien Gerard
Damien Gerard wrote 17 lines
Le Mar 31, 2008 à 6:36 PM, Micha Nelissen a écrit : > Damien Gerard wrote: >>> I tried to use an Interface but I had a nice Segementation fault :) >>> >>> Should I report it as a bug in the mantis ? >>> >>> <foo.pas> >> >> Actually there is the same error with an intermediate class :( > > Segmentation faults in the compiler itself can/should always be > reported. Thanks. Reported as Issue #11075.

It was actually an already known bug :)

Damien Gerard "Intelligence is 10 million rules." -- Douglas Lenat