This is the legacy 4D documentation web site. Documentations are progressively being moved to developer.4d.com |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
4D v19.8
Formula
|
Formula ( formulaExp ) -> Function result | ||||||||
Parameter | Type | Description | ||||||
formulaExp | Expression |
![]() |
Formula to be returned as object | |||||
Function result | Object |
![]() |
Native object encapsulating the formula | |||||
Formula creates a formula object based upon the formulaExp expression. formulaExp can be as simple as a single value or complex, such as a project method with parameters.
Having a formula as an object allows it to be passed as a parameter (calculated attribute) to commands or methods or to be executed from various components without needing to declare them as "shared by components and host database". When called, the formula object is evaluated within the context of the database or component that created it.
The returned formula can be called with the:
C_OBJECT($f)
$f:=Formula(1+2)
$o:=New object("myFormula";$f)
//three different ways to call the formula
$f.call($o) //returns 3
$f.apply($o) //returns 3
$o.myFormula() //returns 3
You can specify the object on which the formula is executed, as seen below in Example 5. The properties of the object can then be accessed via the This command.
If formulaExp uses local variables, their values are copied and stored in the returned formula object when it is created. When executed, the formula uses these copied values rather than the current value of the local variables. Note that using arrays as local variables is not supported.
The object created by Formula can be saved, for example, in a database field or in a blob document.
You can pass parameters to the formula using the standard $1, $2...,$n mechanism. For example, you can write:
C_OBJECT($f)
$f:=Formula($1+" "+$2)
$text:=$f.call(Null;"Hello";"World") //returns "Hello World"
$text:=$f.call(Null;"Welcome to";String(Year of(Current date))) //returns "Welcome to 2019" (for example)
For more convenience, parameters can be omitted in the formula object initialization when the formula is made of a single project method. They can just be passed when the formula is called. For example:
C_OBJECT($f)
$f:=Formula(myMethod)
//Writing Formula(myMethod($1;$2) is not necessary
$text:=$f.call(Null;"Hello";"World") //returns "Hello World"
$text:=$f.call() //returns "How are you?"
//myMethod
C_TEXT($0;$1;$2)
If(Count parameters=2)
$0:=$1+" "+$2
Else
$0:="How are you?"
End if
Parameters are received in $1, $2... within the method, in the order they are specified in the call.
Note: Do not confuse between the $n parameters used in the formula and $n parameters received in the method called in the formula.
A simple formula:
C_OBJECT($f)
$f:=Formula(1+2)
C_OBJECT($o)
$o:=New object("f";$f)
$result:=$o.f() // returns 3
A formula using local variables:
$value:=10
$o:=New object("f";Formula($value))
$value:=20
$result:=$o.f() // returns 10
A simple formula using parameters:
$o:=New object("f";Formula($1+$2))
$result:=$o.f(10;20) //returns 30
A formula using a project method with parameters:
$o:=New object("f";Formula(myMethod))
$result:=$o.f("param1";"param2") // equivalent to $result:=myMethod("param1";"param2")
Using This:
$o:=New object("fullName";Formula(This.firstName+" "+This.lastName))
$o.firstName:="John"
$o.lastName:="Smith"
$result:=$o.fullName() //returns "John Smith"
Calling a formula using object notation:
C_OBJECT($calc;$feta;$robot)
$robot:=New object("name";"Robot";"price";543;"quantity";2)
$feta:=New object("name";"Feta";"price";12.5;"quantity";5)
$calc:=Formula(This.total:=This.price*This.quantity)
//sets the formula to object properties
$feta.calc:=$calc
$robot.calc:=$calc
//call the formula
$feta.calc() // $feta={name:Feta,price:12.5,quantity:5,total:62.5,calc:"[object Formula]"}
$robot.calc() // $robot={name:Robot,price:543,quantity:2,total:1086,calc:"[object Formula]"}
4D Blog - Formula: More power behind simplicity
4D Blog - Formula: Think outside the box
4D Blog - Write your own methods for objects
Formula from string
formula.source
Product: 4D
Theme: Formulas
Number:
1597
Created: 4D v17 R3
Renamed: 4D v17 R6 (Formula)
4D Language Reference ( 4D v19)
4D Language Reference ( 4D v19.1)
4D Language Reference ( 4D v19.4)
4D Language Reference ( 4D v19.5)
4D Language Reference ( 4D v19.6)
4D Language Reference ( 4D v19.7)
4D Language Reference ( 4D v19.8)