Saltar para o conteúdo principal
Versão: Próximo

Formula

4D.Formula objects are created by the Formula or Formula from string commands and allow you execute any 4D expression or code expressed as single-line text.

4D.Formula class objects inherit from the 4D.Function class. Thus, to execute the formula, you can:

  • store a 4D.Formula object in an object property and use the () operator after the property name,
  • or directly call the 4D.Formula object using the call() or apply() function on it.

See examples in the Executing code in Function objects paragraph.

Passar parâmetros a fórmulas

You can pass parameters to your formulas using a sequential parameter syntax based upon $1, $2,...,$n. The numbering of the $ parameters represents the order in which they will be passed to the formula. Por exemplo, pode escrever:

 $f:={message: Formula(ALERT("Hello "+$2+", "+$1))}
$f.message("John";"Smith") //displays "Hello Smith, John"

Ou usando a função .call():

 var $f : 4D.Formula
$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 2026" (for example)

Parâmetros a um único método

Para mais conveniência, quando a fórmula é feita de um único método de projeto, parâmetros podem ser omitidos na inicialização do objeto fórmula. Pode ser passado quando a fórmula for chamada. Por exemplo:

 var $f : 4D.Formula

$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
#DECLARE ($param1 : Text; $param2 : Text)->$return : Text
If(Count parameters=2)
$return:=$param1+" "+$param2
Else
$return:="How are you?"
End if

Parâmetros são recebidos dentro do método, na ordem que são especificados na chamada.

Objecto fórmula

.apply() : any
.apply( thisObj : Object { ; params : Collection } ) : any

executes the function object to which it is applied, passing parameters as a collection, and returns the resulting value
.call() : any
.call( thisObj : Object { ; ...params : any } ) : any

executes the function object to which it is applied, with one or more parameter(s) passed directly, and returns the resulting value
.source : Text
contains the source code of the function as text

.apply()

História
ReleaseMudanças
21 R3Support of 4D.Methods objects
17 R3Adicionado

.apply() : any
.apply( thisObj : Object { ; params : Collection } ) : any

ParâmetroTipoDescrição
thisObjObject->Object to be returned by the This command in the function
paramsCollection->Collection of values to be passed as parameters to the function
Resultadosany<-Value from function execution

Descrição

The .apply() function executes the function object to which it is applied, passing parameters as a collection, and returns the resulting value.

In the thisObj parameter, you can pass a reference to the object to be used as This within the function. Pass Null if you do not want to use This but you want to send parameters.

You can pass a collection to be used as parameters in the function using the optional params parameter:

  • in 4D.Formula objects, parameters are passed in $1...$n in the formula.
  • in other 4D.Function objects such as 4D.Method objects, parameters are passed in declared method parameters.

Note que .apply() é similar a .call() exceto que os parâmetros são passados como coleção. Isso pode ser útil para passar resultados calculados. Isso pode ser útil para passar resultados calculados.

Exemplo 1

 var $f : 4D.Formula
$f:=Formula($1+$2+$3)

$c:=New collection(10;20;30)
$result:=$f.apply(Null;$c) // returns 60

Exemplo 2

 var $calc : 4D.Formula
var $feta; $robot : Object
$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)

$calc.apply($feta) // $feta={name:Feta,price:12.5,quantity:5,total:62.5}
$calc.apply($robot) // $robot={name:Robot,price:543,quantity:2,total:1086}

.call()

História
ReleaseMudanças
21 R3Support of 4D.Methods objects
17 R3Adicionado

.call() : any
.call( thisObj : Object { ; ...params : any } ) : any

ParâmetroTipoDescrição
thisObjObject->Object to be returned by the This command in the function
paramsany->Values to be passed as parameters to the function
Resultadosany<-Value from function execution

Descrição

The .call() function executes the function object to which it is applied, with one or more parameter(s) passed directly, and returns the resulting value.

In the thisObj parameter, you can pass a reference to the object to be used as This within the function.

You can pass values to be used as parameters in the function using the optional params parameter:

  • in 4D.Formula objects, parameters are passed in $1...$n in the formula.
  • in 4D.Method objects, parameters are passed in declared method parameters.

Observe que .call() é semelhante a .apply(), exceto pelo fato de que os parâmetros são passados diretamente.

Exemplo 1

 var $f : 4D.Formula
$f:=Formula(Uppercase($1))
$result:=$f.call(Null;"hello") // returns "HELLO"

Exemplo 2

 $o:=New object("value";50)
$f:=Formula(This.value*2)
$result:=$f.call($o) // devolve 100

.source

História
ReleaseMudanças
21 R3Support of 4D.Methods objects
18 R2Adicionado

.source : Text

Descrição

The .source property contains the source code of the function as text.

The returned value is the original text used to create the 4D.Formula or 4D.Method object but reformatted.

Essa propriedade é somente leitura.

Exemplo

 var $of : 4D.Formula
var $tf : Text
$of:=Formula(String(Current time;HH MM AM PM))
$tf:=$of.source //"String(Current time;HH MM AM PM)"