In the declaration of a function or method, you explicitly define its parameters. Then, when calling that function or method, you can pass fewer parameters to it if some are optional. Or, if you used the declaration with ${n}, the number of passed parameters is variable. In this case, you use the Count Parameters command with a loop over the arguments. This code works flawlessly. Still, it’s a little bit heavy.
You asked us for a lighter way to handle parameters, and we listened. Starting with 4D v19 R5, we provide you with a new command, Copy parameters, allowing you to write a seamless, more elegant, and easier-to-read code.
See the new command in action.
New command
The new command, Copy parameters, returns a collection of the parameters passed to a function or a method. If you pass the “startFrom” parameter to the Copy parameters command, it returns a collection that contains the element from the “startFrom” index.
Examples
Manipulate the parameters as a collection. For example, concatenate a string with the parameters to create an unordered HTML list:
// Class
Function list() -> $html : Text
var $value : Collection
$value:=Copy parameters
$html:="<ul><li>"
$html+=$value.join("</li><li>")
$html+="</li></ul>"
return $html
// Method
$htmlList:=$c.list("Alpha";"Bravo";"Charlie")
// $htmlList: <ul><li>Alpha</li><li>Bravo</li><li>Charlie</li></ul>
Call a different function depending on the first parameter and pass the other parameters to this method:
Function selectTask($task : Text)
Case of
: ($task="Task1")
This.task1(Copy parameters(2))
: ($task="Task2")
This.task2(Copy parameters(2))
End case
Or, call another function on another object with apply() and pass the parameters:
Function doSomething($param : Text; $extraParameters : Variant)
This.delegate.doSomething.apply(This.delegate; Copy parameters)
Next
You can read the documentation for more information. Do not hesitate to share your ideas and examples on the forum.