Sign in Join

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

Mailing lists / fpc-pascal / 2007-12

More problems with generics

3 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 struggling to compile the following code. FPC throws a syntax error where the expression is IMHO correct. Can somebody point me in the right direction?

FPC SVN (rev. 9495) -- FPC 2.2.0 dies with EAccessViolation while compiling the code in question linux 2.6.23 x86

Thank you in advance.
bartek

{$mode objfpc}{$h+}
unit RegisteredCallbacks;
interface
uses classes, sysutils;
type
        { fake TList }
        generic TList<T> = class
        private
                FakeMemory: T;
                function GetElement(const Index: integer): T;
                procedure SetElement(const Index: integer; const AElement: T);
        public
                constructor Create;
                destructor Destroy; override;
                property Element[index: integer]: T read GetElement write SetElement;
        end;
        { TMulticast }
        generic TMulticast<TParam> = class
        type public
                TListener = procedure(const Param: TParam) of object;
                TListenerList = specialize TList<TListener>;
        var private
                Listener: TListener;
                Audience: TListenerList;
        public
                constructor Create;
                destructor Destroy; override;
                procedure Invoke(const Param: TParam);
        end;
implementation

{ fake TList }

constructor TList.Create;
begin
end;
destructor TList.Destroy;
begin
        inherited Destroy;
end;
function TList.GetElement(const Index: Integer): T;
begin
        Result := FakeMemory;
end;
procedure TList.SetElement(const Index: Integer; const AElement: T);
begin
        FakeMemory := AElement;
end;

{ TMulticast }

constructor TMulticast.Create;
begin
        Audience := TListenerList.Create;
end;
destructor TMulticast.Destroy;
begin
        FreeAndNil(Audience);
        inherited Destroy;
end;
procedure TMulticast.Invoke(const Param: TParam);
var
        i: integer;
begin
        Listener(Param); // This *does* compile. Why won't the line below?
        for i := 0 to Audience.Count - 1 do Audience.Element[i](Param);
---------------------------------------------------------------^ (78,64)
end;
end.

% /usr/local/lib/fpc/2.3.1/ppc386 RegisteredCallbacks.pp Free Pascal Compiler version 2.3.1 [2007/12/21] for i386 Copyright (c) 1993-2007 by Florian Klaempfl
Target OS: Linux for i386
Compiling RegisteredCallbacks.pp
RegisteredCallbacks.pp(78,64) Error: Illegal expression RegisteredCallbacks.pp(78,64) Fatal: Syntax error, ";" expected but "(" found Fatal: Compilation aborted
[1028]
%

Peter Vreman
Show quoted text 9 lines
Hi, I am struggling to compile the following code. FPC throws a syntax error where the expression is IMHO correct. Can somebody point me in the right direction? FPC SVN (rev. 9495) -- FPC 2.2.0 dies with EAccessViolation while compiling the code in question linux 2.6.23 x86

Please try r9521 of FPC 2.3.1

Peter

bartek
to Peter Vreman
Peter Vreman wrote 12 lines
> Hi, > > I am struggling to compile the following code. FPC throws a syntax error where the expression is > IMHO correct. > Can somebody point me in the right direction? > > FPC SVN (rev. 9495) -- FPC 2.2.0 dies with EAccessViolation while compiling the code in question > linux 2.6.23 x86 > Please try r9521 of FPC 2.3.1

o_O works. Great! Thank you very much. :)

bartek