The 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:=Formula(OBJECT Get pointer(Object named;$1))
When I want to assign or retrieve the value of my check box, I simply write:
(interface.pointer("checkbox"))->:=0
(interface.pointer("checkbox2"))->:=0
Or, if I need to set thousands of check boxes, I can create a formula to make things simple:
interface.setCheckbox:=Formula((OBJECT Get pointer(Object named;$1))->:=$2)
To set a series of check boxes, I can simply write:
interface.setCheckbox("checkbox1";0)
interface.setCheckbox("checkbox2";1)
interface.setCheckbox("checkbox3";0)
interface.setCheckbox("checkbox4";1)
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:=Formula(SET DATABASE PARAMETER(Tips delay;1))
interface.tips1sDelay:=Formula(SET DATABASE PARAMETER(Tips delay;60))
Then, when you want to change the value, you just call the formula:
interface.tips1sDelay()
// or
interface.tipsNoDelay()
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";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 (\
Formula(Not(Undefined($o)));\
Formula($o.value#Null);\
Formula($o.value<5)\
))
// to do
End if
For the _AND method, very usual and simple code:
$0:=True
For ($i;1;Count parameters;1)
$0:=${$i}.call()
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!