Stop press! Accessing parameters not being passed is … possible

Automatically translated from English

A method or function can have optional parameters. Sometimes the function is called with two parameters, and other times with three parameters. We’ve all dreamed of accessing the parameters without counting the parameters, checking if the parameter has been passed, or simply not having to check if the variable associated with the parameter has been initialized. This could make the code much simpler, and much more readable, without all the “If” or “Count parameters” lines.

Well, dream no more, and switch to 4D v19 R3! Managing method and function parameters has never been easier.

Access the parameters

Now, for parameters you have declared in a function or method’s prototype, if the parameter is not passed during the call, the variable is instantiated to the empty value. Even in compiled mode, you no longer have a runtime error for this. On this documentation page, you have a table with the correspondence for each type of variable.

In the following example, the function uses the $param1 and $param2 parameters without a check. If, when calling the function, the $param1 and/or $param2 parameters are not passed, 4D instantiates the text variable to an empty string:

// "concate" function class
Function concate ($param1 : Text ; $param2 : Text)
 Alert($param1+" "+$param2)

// "method1" method
$class.concate("Hello";" world") // Display: "Hello world"
$class.concate("Hello") // Display: "Hello "
$class.concate() // Display: " "

Be careful. In order for 4D to instantiate your variable, it needs to know the type of that variable. If you use the ${i} notation and you haven’t passed and declared the parameter, you will always get an execution error in compiled mode.

Initialize the parameters

We’ve just seen that if a parameter is not passed, its associated variable is instantiated to the empty value. So now you can use the Choose command to directly define a value if the parameter is not passed.

Here is an example with a method and the #DECLARE instruction:

// "myAlert" method
#DECLARE($param1 : Text ; $param2 : Text)
$param1:=Choose(Count parameters>=1 ; $param1 ; "Hello")
$param2:=Choose(Count parameters>=2 ; $param2 ; "world")
Alert($param1+" "+$param2)

// "method1" method
myAlert() // Display: "Hello world"

What about the components?

To benefit from this simplification, the method called and the calling method must be recompiled. So in the case of a method shared between the host base and a component, both projects need to be recompiled.

But, how about compiled components, whose sources I don’t have? Don’t worry, you can still use them as before. No need to change your code.

What’s next?

This new feature gives you plenty of ideas for your methods and functions. Feel free to share your tips on the forum.

Vanessa Talbot

• Product Owner •

Vanessa Talbot joined 4D Program team in June, 2014. As a Product Owner, she is in charge of writing the user stories then translating it to functional specifications. Her role is also to make sure that the feature implementation delivered is meeting the customer need.

Since her arrival, she has worked to define key features in 4D. She has worked on most of preemptive multi-threading new features and also on a very complex subject: the new architecture for engined application. Vanessa has a degree from Telecom Saint-Etienne. She began her career at the Criminal Research Institute as a developer for the audiovisual department. She has also worked in media and medical fields as expert in technical support, production as well as documenting new features.