Passing Single Values and Blocks to Functions
Author: Gregg Irwin
Here is a tip for creating functions that allow both a single value or a block of values as an argument. For example if you create a function called touch, you may want to allow:
In REBOL a function interface lets you specify more than one datatype for a parameter, and you can also use REBOL pseudotypes (like any-block!) to give you even more flexibility.
This function will accept a file!, block!, hash!, list!, or paren! value (the last four all fall under the any-block! type).
When you pass a block to the function, your code can iterate over it with functions like FOREACH.
But, if a single file is passed, the code above will fail. The FOREACH needs a series of files, not a single file.
The simple solution is to put the single file into a block before doing the FOREACH. The COMPOSE function is a handy way to do that:
So, now you can create a function with a flexible interface but keep it simple without special case handling for different datatypes.
Of course, here we assume that a block passed to us contains file! values and that the files aren't read-only in order for SET-MODES to work.