Raton Kumar
Raton Kumar

Reputation: 93

How to assign function to Rebol struct member

I first define a function ADD:

add: func [ a [integer!] b [integer!] ] [a + b]

Then a struct:

s: make struct! [
    sadd [ function! ]
] add

But Rebol struct does not support the FUNCTION! datatype. How can I assign a function to a Rebol struct?

Upvotes: 1

Views: 112

Answers (1)

Graham Chiu
Graham Chiu

Reputation: 4886

Callbacks are an alpha feature of Rebol2. See Carl's article for documentation.

Essentially, if you have a dll such as test-lib.dll where the test function takes two integers and returns them again unchanged

extern "C"
MYDLL_API int test(int a, int b, int (*pFunc)(int, int))
{
   int result = pFunc(a, b);
   return result;
}

you would write the calling function from Rebol like this

test: make routine! [
    a [int]
    b [int]
    c [callback [int int return: [int]]]
    return: [int]
] test-lib "test"

So, this test function takes two integers as parameters and a third parameter which is a Rebol function to be used as a callback. The callback in the routine! is a keyword. The block specification is automatically turned into a structure!

The callback function is written like this which takes the two integers returned by the library call, adds them, and returns them.

add-it: func [a b][return a + b]

And it's then used like this

>> test 1 2 :add-it
== 3

Upvotes: 2

Related Questions