Sign in Join

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

Mailing lists / fpc-pascal / 2007-12

Problems with generics

5 messages · 2 people · –

Reply by email Subscribe to this list Your subscription

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

bartek

Hi,

I am toying around with generics and i got stuck when writing somewhat more advanced generic code. This code has already been once submitted by me as a bug(#0010247) report because fpc 2.2.0 crashes when trying to compile this. But this time I got futher with the latest svn(9488) compile. The error message(see below) which i got is obvious but i have no idea what to do instead of writing TBucket.PNode. I hope somebody can enlighten me. Thank you in advance.

----------- 8< *snip, snip* ------------------------- { TList }

        generic TList<T> = class
                [...]
        end;
        { TLinkedList }
        generic TLinkedList<T> = class
        type
        public
                PNode = ^TNode;
                TNode = record
                        Previous,
                        Next: PNode;
                        Element: T;
                end;
        var
                [...]
        end;
        { THashTable }
        generic THashTable<T> = class
        type
        public
                THashNode = record
                        Hash: LongWord;
                        Key: ShortString;
                        Element: T;
                end;
                TBucket = specialize TLinkedList<THashNode>;
                TBucketList = specialize TList<TBucket>;
        var
        private
                BucketList: TBucketList;
                procedure InitBucketList(const TableSize: integer);
                procedure ReHash(const TableSize: integer);
                function GetElement(const Key: ShortString): T;
                procedure SetElement(const Key: ShortString; const AElement: T);
                function GetTableSize: integer;
                procedure SetTableSize(const AValue: integer);
        public
                constructor Create;
                destructor Destroy; override;
                function HashOf(const Key: ShortString): DWord;
                function BucketNodeOf(const Hash: DWord): TBucket.PNode;
----------------------------------------------------------^^^^^^^^^^^^^^ DataContainers.pp(104,66) Fatal: Syntax error, ";" expected but "." found
                function Extract(const Hash: DWord): T;
                function Append(const ANode: TBucket.PNode; const AHash: DWord):
                property Element[Key: ShortString]: T read GetElement write SetElement;
                property TableSize: integer read GetTableSize write SetTableSize;
        end;

----------- 8< *snip, snip* -------------------------

bartek

Micha Nelissen
to bartek
bartek wrote
function HashOf(const Key: ShortString): DWord; function BucketNodeOf(const Hash: DWord): TBucket.PNode; DataContainers.pp(104,66) Fatal: Syntax error, ";" expected but "." found

I'd say this is a bug, and your syntax is correct. Update the existing bug report, perhaps?

Micha

bartek
to Micha Nelissen
Micha Nelissen wrote 14 lines
bartek wrote: > function HashOf(const Key: ShortString): DWord; > > function BucketNodeOf(const Hash: DWord): TBucket.PNode; > DataContainers.pp(104,66) Fatal: Syntax error, ";" expected but "." found I'd say this is a bug, and your syntax is correct. Update the existing bug report, perhaps? Micha _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/mailman/listinfo/fpc-pascal

I am not quite sure whether my synatx is correct. IMHO explictly defining the type feels more pascalish. Having more time at hand i have written a much shorter example code which shows in what way such a type should be defined.

Now I get:

[1004]
% fpc test3.pp
Free Pascal Compiler version 2.3.1 [2007/12/28] for i386 Copyright (c) 1993-2007 by Florian Klaempfl
Target OS: Linux for i386
Compiling test3.pp
test3.pp(18,32) Error: Error in type definition
test3.pp(26,34) Error: Type "TPT" is not completely defined test3.pp(47,18) Error: Identifier not found "TPT"
test3.pp(49,24) Error: Identifier not found "Data" test3.pp(54) Fatal: There were 4 errors compiling module, stopping Fatal: Compilation aborted
Error: /usr/bin/ppc386 returned an error exitcode (normal if you did not specify a source file to be compiled)

"Error in a type definition". I do not know whether fpc is supposed to support this kind if structures. If it is than it is a bug otherwise, no.

Code:

{$mode objfpc}{$h+}
program test3;
uses classes, sysutils;
type
        generic TNode<T> = class
        type public
                PT = ^T;
        var private
                Data: T;
        public
                constructor Create;
                destructor Destroy; override;
        end;
        generic TContainer<T> = class
        type public
                TTNode = specialize TNode<T>;
                TPT = TTNode.PT;
        var
        private
                Data: TTNode;
        public
                constructor Create;
                destructor Destroy; override;
                function Get: TPT;
        end;
constructor TNode.Create;
begin
end;
destructor TNode.Destroy;
begin
        inherited Destroy;
end;
constructor TContainer.Create;
begin
end;
destructor TContainer.Destroy;
begin
        inherited Destroy;
end;
function Get: TPT;
begin
        result := @Data.Data;
end;
begin
end.
Micha Nelissen
to bartek
bartek wrote
I am not quite sure whether my synatx is correct. IMHO explictly defining the type feels more pascalish. Having more time at hand i have written a much shorter example code which shows in what way such a type should be defined.

I think that it is the same type in a different namespace. Like using "unit.type" syntax.

Micha

bartek
to Micha Nelissen
Micha Nelissen wrote
bartek wrote: > I am not quite sure whether my synatx is correct. IMHO explictly defining

the

> type feels more pascalish. Having more time at hand i have written a much > shorter example code which shows in what way such a type should be

defined.

Show quoted text 9 lines
I think that it is the same type in a different namespace. Like using "unit.type" syntax. Micha _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/mailman/listinfo/fpc-pascal

Yes you are right, I did not think of that. I think a seperate bugreport is appropriate, since this bug is not even really related to the previous bug. The last one made the 2.2.0 compiler crash, this one is a missing feature in the svn trunk.

Reported as #0010512 with unit.type/(generic)class.type as expected behaviour.

bartek