Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass functions as parameters in REBOL

Tags:

rebol

I have attempted to pass a function as a parameter in the REBOL programming language, but I haven't figured out the correct syntax yet:

doSomething: func [a b] [
    a b
    a b
]

doSomething print "hello" {This should pass print as the first argument and "hello" as the second argument.}

This produces an error, since the print function is being called instead of being passed:

hello
*** ERROR
** Script error: doSomething does not allow unset! for its a argument
** Where: try do either either either -apply-
** Near: try load/all join %/users/try-REBOL/data/ system/script/args...

Is it possible to pass the print function as a parameter instead of calling the print function?

like image 446
Anderson Green Avatar asked Oct 17 '25 13:10

Anderson Green


1 Answers

I've found the solution: I only need to add : before the name of the function that is being passed as a parameter.

Here, the :print function is being passed as a parameter instead of being invoked with "hello" as its argument:

doSomething: func [a b] [
    a b
    a b
]

doSomething :print "hello" {This should pass print as the first argument and "hello" as the second argument.}
like image 198
Anderson Green Avatar answered Oct 19 '25 10:10

Anderson Green