Einzelnen Beitrag anzeigen

Benutzerbild von himitsu
himitsu

Registriert seit: 11. Okt 2003
Ort: Elbflorenz
43.152 Beiträge
 
Delphi 12 Athens
 
#2

Re: Allgem. Typ für prozedurale Typen, wie TClass für Klasse

  Alt 2. Jan 2010, 11:53
Sowas gib es nicht,

aber warte mal 'nen Sekündchen, es gibt da eventuell einen Trick.
> praktisch wie bei TIndex aus meinem himXML ... muß aber mal probieren ob's wirklich so klappt


[add]

Delphi-Quellcode:
Type TMyProcA = Procedure({Parameter});
  TMyProcB = Procedure({Parameter}) of Object;
  TMyMultiProc = Record
    Class Operator Implicit(Const Value: TMyProcA): TMyMultiProc;
    Class Operator Implicit(Const Value: TMyProcB): TMyMultiProc;
  Private
    Type TProcType = (ptProcA, ptProcB);
    Case ProcType: TProcType of
      ptProcA: (ProcA: TMyProcA);
      ptProcB: (ProcB: TMyProcB);
  End;

Class Operator TMyMultiProc.Implicit(Const Value: TMyProcA): TMyMultiProc;
  Begin
    Result.ProcType := ptProcA;
    Result.ProcA := Value;
  End;

Class Operator TMyMultiProc.Implicit(Const Value: TMyProcB): TMyMultiProc;
  Begin
    Result.ProcType := ptProcB;
    Result.ProcB := Value;
  End;
Delphi-Quellcode:
Procedure Test(Const Proc: TMyMultiProc);
  Begin
    Case Proc.ProcType of
      ptProcA: Proc.ProcA({Parameter});
      ptProcB: Proc.ProcB({Parameter});
    End;
  End;
Test und TMyMultiProc müssen in der selben Unit deklariert sein,
aber notfalls kann man auch einfach das Private weglassen.

So müßte es möglich sein Test Prozeduren von beiden Typen zu übergeben.

Wobei man dieses Beispiel auch ganz einfach per Overloading hätte lösen können,
welches auch schon vor Delphi 2006 funktioniert.
Delphi-Quellcode:
Procedure Test(Const Proc: TMyProcA); Overload;
Procedure Test(Const Proc: TMyProcB); Overload;
Garbage Collector ... Delphianer erzeugen keinen Müll, also brauchen sie auch keinen Müllsucher.
my Delphi wish list : BugReports/FeatureRequests
  Mit Zitat antworten Zitat