Sign in Join

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

Mailing lists / fpc-devel / 2007-09

Problem (bug?) with generics

6 messages · 3 people · –

Reply by email Subscribe to this list Your subscription

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

Giulio Bernardi

Hi,
I have a problem with generics (see code at the end of the mail). I'm trying to use a record as the type of a generic class, but the code doesn't compile because fpc complains with: "Error: Illegal qualifier"
on lines where I use the record fields.
I think that the compiler should raise this error only if the class is specialized with an incompatible type.
code:

===========================================================

program genbug;
{$MODE OBJFPC}
uses
  Classes, SysUtils;
type
  TRec32 = packed record
    data1 : longword;
    data2 : longword;
  end;
  PRec32 = ^TRec32;
  TRec64 = packed record
    data1 : qword;
    data2 : qword;
  end;
  PRec64 = ^TRec64;
  generic TGenClass<_TPRec_,_TRec_> = class
  public
    constructor Create;
  end;
  TClass32 = specialize TGenClass<PRec32,TRec32>;
  TClass64 = specialize TGenClass<PRec64,TRec64>;
  constructor TGenClass.Create;
  var p : _TPRec_;
  begin
    GetMem(p,sizeof(_TRec_));
    p^.data1:=1;
    p^.data2:=2;
    FreeMem(p);
  end;
begin
end.

===========================================================

FPC raises an error on these lines:
    p^.data1:=1;
    p^.data2:=2;
Tried with 2.2.0 and 2.3.1 (svn revision 8423).

Is this a bug (should I report this to mantis?) or a feature?

bye,
Giulio

_________________________________________________________________ Don't just search. Find. Check out the new MSN Search! http://search.msn.com/

Reply by email

Micha Nelissen
to Giulio Bernardi
Giulio Bernardi wrote 6 lines
FPC raises an error on these lines: p^.data1:=1; p^.data2:=2; Tried with 2.2.0 and 2.3.1 (svn revision 8423). Is this a bug (should I report this to mantis?) or a feature?

How are you specializing this generic ?

Micha

Reply by email

Micha Nelissen
to Micha Nelissen
Micha Nelissen wrote 9 lines
Giulio Bernardi wrote: > FPC raises an error on these lines: > p^.data1:=1; > p^.data2:=2; > Tried with 2.2.0 and 2.3.1 (svn revision 8423). > > Is this a bug (should I report this to mantis?) or a feature? How are you specializing this generic ?

Never mind, it's in the lines before. Seems like fpc doesn't allow dereferencing a variable of a generic type in any way. The expression p^ will generate the error.

Micha

Reply by email

Bartosz Bogacz
to Giulio Bernardi: I have a problem with generics (see code at the end of the mail). I'm trying to use a…

On Thursday 13 September 2007 16:47:38 Giulio Bernardi wrote: You can make your own types in the generic class: (I changed your source a bit)

Show quoted text 14 lines
=========================================================== program genbug; {$MODE OBJFPC} uses Classes, SysUtils; type TRec32 = packed record data1 : longword; data2 : longword; end;
//   PRec32 = ^TRec32;
Show quoted text 5 lines
TRec64 = packed record data1 : qword; data2 : qword; end;
//   PRec64 = ^TRec64;

generic TGenClass<_TRec_> = class
protected type
_TPRec_: ^_TRec_

Show quoted text 22 lines
public constructor Create; end; TClass32 = specialize TGenClass<TRec32>; TClass64 = specialize TGenClass<TRec64>; constructor TGenClass.Create; var p : _TPRec_; begin GetMem(p,sizeof(_TRec_)); p^.data1:=1; p^.data2:=2; FreeMem(p); end; begin end. ===========================================================

*Should* work now, I just wrote it form top of my head and did not test it.

Reply by email

Giulio Bernardi
to Micha Nelissen: Never mind, it's in the lines before. Seems like fpc doesn't allow dereferencing a…
Never mind, it's in the lines before. Seems like fpc doesn't allow dereferencing a variable of a generic type in any way. The expression p^ will generate the error.

Sorry I made the wrong example. The problem isn't in the dereferencing of the pointer,
but in accessing record fields. Here is simpler example: ==============================================
program genbug;

{$MODE OBJFPC}
uses
  Classes, SysUtils;
type
  TRec32 = packed record
    data1 : longword;
    data2 : longword;
  end;
  TRec64 = packed record
    data1 : qword;
    data2 : qword;
  end;
  generic TGenClass<_TRec_> = class
  public
    constructor Create;
  end;
  TClass32 = specialize TGenClass<TRec32>;
  TClass64 = specialize TGenClass<TRec64>;
  constructor TGenClass.Create;
  var rec : _TRec_;
  begin
    rec.data1:=1;
    rec.data2:=2;
  end;
begin
end.
==============================================

FPC complains in

    rec.data1:=1;
    rec.data2:=2;

because rec isn't fully defined, so it finds no field data1 or data2 in rec. However, I think it should complain only if I try to specialize TGenClass with
a type that doesn't have data1 and data2 fields/members/properties.

So I am wondering if this is the correct behaviour, or if it is a bug, or if it
is something that is not implemented now but will be in the future.

bye,
Giulio

Reply by email

Micha Nelissen
to Giulio Bernardi
Giulio Bernardi wrote
Sorry I made the wrong example. The problem isn't in the dereferencing of the pointer, but in accessing record fields. Here is simpler example:

Perhaps both, because I tried to change the GetMem line to:

  constructor TGenClass.Create;
  var p : _TPRec_;
  begin
    GetMem(p,sizeof(p^));
    p^.data1:=1;

and now I had 3 compile errors :-). I think this should work, and it's a bug (or both depending on point of view).

Micha

Reply by email