A Fresh Approach to Writing Variadic Functions

Many of you are familiar with using methods or functions that can accept an unspecified number of parameters, often referred to as variadic functions.

In our ongoing process to improve your development experience, 4D v20 R3 streamlined the declaration of these methods by introducing ellipsis, making your code more straightforward and easier to work with.

In your function or method declaration, you can now employ “…” in your last parameter to specify that the number of parameters is variable. These suspension points can be supplemented by a type. This type is then used by 4D to check the type of parameters received by the function or method.

For example, if you want to create a SumNumbers method that sums all the numbers passed as parameters:

#DECLARE( ...  : Real) : Real
var $number; $total : Real
var $i : Integer

For ($i; 1; Count parameters)
	$total+= ${$i}
End for 

return $total

This method can be called with a variable number of parameters:

$total1:=SumNumbers // returns 0

$total2:=SumNumbers(1; 2; 3; 4; 5) // returns 15

With this syntax, 4D now checks the parameter types for you:

$total3:=SumNumbers(1; 2; "test"; 4; 5) // returns an error

But if you want to accept any type, use “…” without type:

function AcceptAllType(...)
var $value : variant

For each ($value; Copy parameters())
   // your code here
End for each 

Take advantage of this new syntax to create flexible functions or methods, and feel free to explore these features in greater detail by referring to the documentation!

Fabrice Mainguené
• Product Owner •Fabrice Mainguené joined 4D Program team in November, 2016. As a Product Owner, he is in charge of writing the user stories then translating it to functional specifications. His role is also to make sure that the feature implementation delivered is meeting the customer need.After obtaining a Bachelor degree in Computer Science at CNAM, Fabrice joined a small software publishing company as a Windev developer. Then he worked for different companies in industry and trade areas as a Windev and web developer as well as technical advisor on new features.