Generics

classic Classic list List threaded Threaded
14 messages Options
Reply | Threaded
Open this post in threaded view
|

Generics

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
[hidden email]

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





_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Generics

ik-6
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

On Mon, Mar 31, 2008 at 4:00 PM, Damien Gerard <[hidden email]> 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
>  [hidden email]
>
>  "Intelligence is 10 million rules."
>     -- Douglas Lenat
>
>
>
>
>
>  _______________________________________________
>  fpc-pascal maillist  -  [hidden email]
>  http://lists.freepascal.org/mailman/listinfo/fpc-pascal
>



--
http://ik.homelinux.org/
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Generics

Damien Gerard

Le Mar 31, 2008 à 3:20 PM, ik a écrit :
> 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 :)
>

:)


> Ido
>
> On Mon, Mar 31, 2008 at 4:00 PM, Damien Gerard  
> <[hidden email]> 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
>> [hidden email]
>>
>> "Intelligence is 10 million rules."
>>    -- Douglas Lenat
>>
>>
>>
>>
>>
>> _______________________________________________
>> fpc-pascal maillist  -  [hidden email]
>> http://lists.freepascal.org/mailman/listinfo/fpc-pascal
>>
>
>
>
> --
> http://ik.homelinux.org/
> _______________________________________________
> fpc-pascal maillist  -  [hidden email]
> http://lists.freepascal.org/mailman/listinfo/fpc-pascal



--
Damien Gerard
[hidden email]

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





_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Generics

Damien Gerard
In reply to this post by Damien Gerard

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
[hidden email]

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





_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Generics

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

Ido

On Mon, Mar 31, 2008 at 4:27 PM, Damien Gerard <[hidden email]> 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
>  [hidden email]
>
>  "Intelligence is 10 million rules."
>     -- Douglas Lenat
>
>
>
>
>
>  _______________________________________________
>  fpc-pascal maillist  -  [hidden email]
>  http://lists.freepascal.org/mailman/listinfo/fpc-pascal
>


--
http://ik.homelinux.org/

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Generics

Damien Gerard

Le Mar 31, 2008 à 3:59 PM, ik a écrit :
> 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 ?

> Ido
>
> On Mon, Mar 31, 2008 at 4:27 PM, Damien Gerard  
> <[hidden email]> 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
[hidden email]

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






_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal

foo.pas (4K) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Generics

Marco van de Voort
In reply to this post by Damien Gerard
> Le Mar 31, 2008 ? 3:00 PM, Damien Gerard a ?crit :
>
> I forgot : fpc 2.2.0 and fpc 2.2.1. May be I should upgrade to 2.3.x ?

Afaik you need 2.3.x. A recent one (of this year).
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Generics

Peter Vreman
In reply to this post by 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 ?

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

Peter

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Generics

Damien Gerard

Le Mar 31, 2008 à 6:03 PM, Peter Vreman 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 ?
>
> 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
[hidden email]

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






_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal

foo.pas (4K) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Generics

Damien Gerard
>
> 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
[hidden email]

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





_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Generics

Micha Nelissen
In reply to this post by Damien Gerard
Damien Gerard wrote:
> Oki I understand.
> I tried to use an Interface but I had a nice Segementation fault :)

I'm not sure how FParent.ItemByDefault is going to work?

Micha
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Generics

Micha Nelissen
In reply to this post by Damien Gerard
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.

Micha
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Generics

Damien Gerard

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.


--
Damien Gerard
[hidden email]

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





_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Generics

Damien Gerard

Le Mar 31, 2008 à 10:06 PM, Damien Gerard a écrit :

>
> 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
[hidden email]

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





_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal