Thema: Delphi Die Property Falle...

Einzelnen Beitrag anzeigen

Benutzerbild von Mavarik
Mavarik

Registriert seit: 9. Feb 2006
Ort: Stolberg (Rhld)
4.128 Beiträge
 
Delphi 10.3 Rio
 
#1

Die Property Falle...

  Alt 24. Nov 2014, 10:52
Hallo Zusammen!

Titel war:"Anonyme Proceduren mit und ohne Parameter Spass!"

Gegeben ist ein kleines Interface:
Delphi-Quellcode:
  TMyProc = TProc;
  TMyProc2 = TProc<byte>;

  IFOO = Interface
    ['{7DE4C519-DC13-438A-8FA9-F9A6C10EE9BB}']
    Procedure SetOne(AValue:TMyProc);
    Function GetOne:TMyProc;
    Procedure SetTwo(AValue:TMyProc2);
    Function GetTwo:TMyProc2;
    Procedure SetProgs(A : TMyProc;B:TMyProc2);
    Property One : TMyProc read GetOne write SetOne;
    Property Two : TMyProc2 read GetTwo write SetTwo;
  End;
Das Ganze wird initialisiert mit:
Delphi-Quellcode:
var
  Foo : IFoo;
begin
  Foo := TFoo.Create;

  Foo.SetProgs(Procedure
                 begin
                   Memo1.Lines.Add('Check OK One');
                 end,
               Procedure (B:byte)
                 begin
                   Memo1.Lines.Add('Check OK Two');
                 end);
  GFoo := Foo; // Simuliert eine TList
end;
GFoo ist eine globale Interfacevariable die im "eigentlichen" Programm aus einer TList<IFoo> besteht.

Jetzt wird es lustig. Wir machen mal 4 unterschiedliche Aufrufe:

Delphi-Quellcode:
GFoo.Two(43);
GFoo.Two;
GFoo.One;
GFoo.One();
Alle 4 Aufrufe lassen sich compilieren... Was auch logisch ist.
Kleines Ratespiel? Welcher Aufruf funktioniert und welcher nicht?

Logisch GFoo.Two(43) und GFoo.One() <- besonders geil
und warum GFoo.Two und GFoo.One nicht? Besonders da GFoo.One ja keinen Parameter hat!

...

Na weil GFoo.Two und GFoo.One eigentlich GFoo.GetOne und GFoo.GetTwo ist und somit nur ein
Funktionsaufruf ist, bei dem das Ergebniss "verpufft" {$X+}

Wie schnell kann man hier in diese Falle geraten! Ich habe 3h gesucht..

Grüsse Mavarik

Geändert von Mavarik (24. Nov 2014 um 11:05 Uhr)
  Mit Zitat antworten Zitat