More problems with generics

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

More problems with generics

Bartek-9
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] bartek@banana:~/Dev/bLibrary
%
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: More problems with generics

Peter Vreman
> 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

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

Re: More problems with generics

Bartek-9
On Sunday 23 December 2007 21:59:04 Peter Vreman wrote:

> > 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
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal