New formula: More power behind simplicity

Automatically translated from English

The New formula command is available since 4D v17 R3. So far you might have thought of assigning a formula as a method to an object or building smart objects that can calculate values or do anything you need. That’s really great, and there’s more than meets the eye!

I had a great demonstration from Vincent de Lachaux – Developer and expert 4D – on how he uses this command. For this blog post, I compiled different scenarios from that demo to give you insight on a different dimension of this command.

Example 1 – To simplify an existing command

Some 4D commands are used hundreds of thousands of times in the code. We often pass the same parameters except one.

Let’s take the simple example of the OBJECT get pointer command. I want to initialize the values of my check boxes, so I write:

(OBJECT get pointer(Object named;"checkbox"))->:=0

Or, I write a formula that takes the name of my object as a parameter:

interface.pointer:=New formula(OBJECT Get pointer(Object named;$1))

When I want to assign or retrieve the value of my check box, I simply write:


Or, if I need to set thousands of check boxes, I can create a formula to make things simple:

interface.setCheckbox:=New formula((OBJECT Get pointer(Object named;$1))->:=$2)

To set a series of check boxes, I can simply write:


Easier to read – and much easier to write, as I don’t need to count parenthesis…

Example 2 – With a database parameter

In your code, set the same database parameter several times with different values. Let’s take a concrete example with delay control tips. You may want to display the tips without delay in some parts of your forms, and to wait 1s in other parts. Each time you use the database parameter, you must remember the value or look for it in another piece of code.

Why not creating a formula?

interface.tipsNoDelay:=New formula(SET DATABASE PARAMETER(Tips delay;1))
interface.tips1sDelay:=New formula(SET DATABASE PARAMETER(Tips delay;60))

Then, when you want to change the value, you just call the formula:

// or

Example 3 – Use formula as a variant

You can create a formula that uses the values passed as parameters of a method (i.e. $1, $2) but, in the method in which you create your formula, you do not need to type $1 and $2. The variables are typed when the formula is executed. You can write generic methods or formulas.

Here is a simple example showing that the type is defined at execution. 

$obj:=New object("addition";New formula($1+$2))

$number:=$obj.addition(1;2) // returns 3
$text:=$obj.addition("hello ";"world") // returns hello world

Example 4 –  Test a series of conditions

If you make a sequence of conditions, such as If (() & ()), 4D always evaluates all conditions. It does not stop and keeps evaluating the following ones. If some conditions can only be evaluated if the leftmost are true, you are forced to make a deep nested list of if conditions, to check if the object is valid before testing the attribute. This is how it goes:

If (Not(Undefined($o))
   If ($o.value#Null)
      If ($o.value<5)
         // to do
      End if
   End if
End if

In this example, we had “only” 3 conditions, but you can easily notice that it becomes more and more complex. What about writing everything in a single condition? Why not writing a method that executes your formulas one after the other? If the result is True, you evaluate the following formula, if the result is false, you stop and return False.

If (_AND (\
   New formula(Not(Undefined($o)));\
   New formula($o.value#Null);\
   New formula($o.value<5)\
   // to do
End if

For the _AND method, very usual and simple code:

For ($i;1;Count parameters;1)
   If (Not($0))
      $i:=MAXLONG-1 // Stop if the condition fails
   End if
End for

I hope these examples will give you insight on other ways to use this command. Feel free to share your formulas on the 4D forum… I’m sure you have plenty of ideas!


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.