Results 1 to 5 of 5

Thread: Function als Parameter ?

  1. #1
    Hobby fröbelaar
    Join Date
    Nov 2002
    Location
    Goes
    Posts
    458

    Function als Parameter ?

    Hallo allemaal,

    Ik heb een aparte unit met een functie en die wil ik aanroepen met een functie als parameter.

    Code:
    unit: My_Unit;
    
    interface
    uses  // vanalles
    
    type TFuncParam = function(MyParam : TParam): TResult;
    
    type TMyClass  = class
    
    constructor create(MaakWat);
    procedure DoeWat (fn : TFuncParam);
    end;
    
    implementation
    
    constructor TMyClass.Create(Maakwat);
    
    
    procedure TMyClass.DoeWat(fn : TFuncparam);
    begin
    Myval := fn(MyVal);
    end;
    en dan zou ik dit in mn mainunit aan willen roepen met een functie in de mainunit als parameter.

    Code:
    function TMainform.DoeIets(MyParam : TParam):TResult;
    begin
     result := waanzinnig;
    end;
    
    procedure TMainform.ButtonClick(Sendr: TObject);
    var fn : TFuncParam;
    begin
     MyClass.DoeWat(fn(DoeIets?);
    end;
    Maar geen idee hoe?
    Jullie wel?
    Greetzz Jacco

  2. #2
    Hobby fröbelaar
    Join Date
    Nov 2002
    Location
    Goes
    Posts
    458
    Hoever je kan komen als je verder gaat met zoeken terwijl je op een antwoord wacht?
    Nou..... soms bij verschillende spelden in verschillende hooibergen.
    Maar soms...... bij de goede speld in de goede hooiberg.

    Code:
    type  TFuncParam = reference to function(MyParam: TMyParam):TResult;
    HIERO heb ik het antwoord gevonden voor het geval er meerdere mensen dezelfde vraag hebben.
    Greetzz Jacco

  3. #3
    mov rax,marcov; push rax marcov's Avatar
    Join Date
    Apr 2004
    Location
    Ehv, Nl
    Posts
    10,357
    Dat is niet de logische oplossing. De logische oplossing is als je methods van een class wil doorgeven, dat je TFuncParam als "of object" definieert:


    Delphi Code:
    1. type TFuncParam = function(MyParam : TParam): TResult of object;

  4. #4
    Als je specifiek methods door wil geven wel ja, maar is reference to function verder niet flexibeler?
    1+1=b

  5. #5
    mov rax,marcov; push rax marcov's Avatar
    Join Date
    Apr 2004
    Location
    Ehv, Nl
    Posts
    10,357
    Quote Originally Posted by GolezTrol View Post
    Als je specifiek methods door wil geven wel ja, maar is reference to function verder niet flexibeler?
    Via javascript aanroepen is vast nog flexibeler. En langzamer.

    Het punt is echter dat ik de indruk kreeg dat OP het "of object" punt niet eens wist. Eerst de basis oplossing, dan verder uitbouwen. Een hoop lagen tussen voegen (zoals de impliciete interface van een "reference to" closure per gebruik) zonder het te weten of het uit te kunnen buiten is zinloos.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •