[Lazarus] array[0..n] of class of TSomething
Bernd
prof7bit at googlemail.com
Tue Jul 12 17:35:45 CEST 2011
Hi,
I have the following (I don't yet know whether this is only
pseudo-code to demonstrate what I mean (I have clearly done too much
Python during the last year) or whether something like this or similar
would actually work, I haven't tested it yet, although it compiles
without errors), or whether this is complete nonsense:
implementation
const
MsgTypes : array[0..1] of class of Tp2pMessage = (
Tp2pMessageVersion,
Tp2pMessageGetdata
); // this will be a lot more classes later
and later i have the following:
procedure TPeer.ProcessMessage;
var
Msg : Tp2pMessage;
TMsg : class of Tp2pMessage;
...
begin
...
for TMsg in MsgTypes do begin
if Header.Command = TMsg.GetCommand then begin
Msg := TMsg.Create(Buffer, Header.Length);
Msg.Parse;
Msg.Process;
break;
end;
end;
(GetCommand is a class function).
I cannot use this syntax at all because Lazarus is not happy with with
my array declaration and codetools will only give me the followin
error whenever I use autocompletion:
"test.pas(42,35) Error: procedure or function or constructor or
destructor expected, but of found"
and the cursor is pointing to the second "of" in my array declaration.
Now the question: Is this all complete nonsense what I am trying here
and I am using it wrong or am trying to over-engineer a simple thing?
I'm trying to avoid a spaghetti-like monster-if from hell and want
most of the things that the received messages would do actually let
the messages do it themselves.
is there any possible variation of this design pattern so that Lazarus
will not complain (I haven't actually tested whether the above even
works because some parts are still missing but at least it seems to
compile with FPC 2.5.1.
Bernd
More information about the Lazarus
mailing list