Note that there are some explanatory texts on larger screens.

plurals
  1. POPassing Interface's method as parameter
    primarykey
    data
    text
    <p>Is it possible to pass interface's method as parameters?</p> <p>I'm trying something like this:</p> <pre><code>interface type TMoveProc = procedure of object; // also tested with TMoveProc = procedure; // procedure of interface is not working ;) ISomeInterface = interface procedure Pred; procedure Next; end; TSomeObject = class(TObject) public procedure Move(MoveProc: TMoveProc); end; implementation procedure TSomeObject.Move(MoveProc: TMoveProc); begin while True do begin // Some common code that works for both procedures MoveProc; // More code... end; end; procedure Usage; var o: TSomeObject; i: ISomeInterface; begin o := TSomeObject.Create; i := GetSomeInterface; o.Move(i.Next); // somewhere else: o.Move(i.Prev); // tested with o.Move(@i.Next), @@... with no luck o.Free; end; </code></pre> <p>But it is not working because:</p> <blockquote> <p>E2010 Incompatible types: 'TMoveProc' and 'procedure, untyped pointer or untyped parameter'</p> </blockquote> <p>Of course I can do private method for each call, but that is ugly. Is there any better way?</p> <p>Delphi 2006</p> <hr> <p>Edit: I know that I can pass whole interface, but then I have to specify which function use. I don't want two exactly same procedures with one different call. </p> <p>I can use second parameter, but that is ugly too.</p> <pre><code>type SomeInterfaceMethod = (siPred, siNext) procedure Move(SomeInt: ISomeInterface; Direction: SomeInterfaceMethod) begin case Direction of: siPred: SomeInt.Pred; siNext: SomeInt.Next end; end; </code></pre> <hr> <p>Thanks all for help and ideas. Clean solution (for my Delphi 2006) is Diego's Visitor. Now I'm using simple ("ugly") wrapper (my own, same solution by TOndrej and Aikislave).</p> <p>But true answer is "there is no (direct) way to pass interface's methods as parameters without some kind of provider.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload