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

Function

A 4D.Function object contains a piece of code that can be executed from an object, either using the () operator, or using the apply() and call() functions. 4D proposes three kinds of Function objects:

  • native functions, i.e. built-in functions from various 4D classes such as collection.sort() or file.copyTo().
  • user functions, created in user classes using the Function keyword.
  • formula functions, i.e. functions that can execute any 4D formula.

Objetos de formulários

The Formula and Formula from string commands allow you to create 4D.Function objects to execute any 4D expression or code expressed as text.

Objetos formulário podem ser encapsulados em propriedades objeto:

 var $f : 4D. Function
$f:=New object
$f.message:=Formula(ALERT("Hello world"))

Essa propriedade é uma "função objeto" ou seja uma função que é restrita a seu objeto pai. To execute a function stored in an object property, use the () operator after the property name, such as:

 $f.message() //exibe "Hello world"

Também se admite a sintaxe com parênteses:

 $f["message"]() //exibe "Hello world"

Note que mesmo se não tiver parênteses (ver abaixo), uma função objeto a ser executada deve ser chamda com () parênteses. Chamar só a propriedade de objeto devolverá uma nova referência à fórmula (e não a executará):

 $o:=$f.message //devolve o objeto fórmula em $o

You can also execute a function using the apply() and call() functions:

 $f.message.apply() //exibe "Hello world"

Utilização de parâmetros

You can pass parameters to your formulas using the sequential parameter syntax based upon $1, $2...$n. Por exemplo, pode escrever:

 var $f : Object
$f:=New object
$f.message:=Formula(ALERT("Hello "+$1))
$f.message("John") //exibe "Hello John"

Or using the .call() function:

 var $f : Object
$f:=Formula($1+" "+$2)
$text:=$f.call(Null;"Hello";"World") //retorna "Hello World"
$text:=$f.call(Null;"Welcome to";String(Year of(Current date))) //retorna "Welcome to 2019" (por exemplo)

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.Function

$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.

Resumo

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

executes the formula object to which it is applied and returns the resulting value
.call() : any
.call( thisObj : Object { ; ...params : any } ) : any

executes the formula object to which it is applied and returns the resulting value
.source : Text
contains the source expression of the formula as text

Formula

História
ReleaseMudanças
17 R6Renamed (New formula -> Formula)
17 R3Adicionado

Formula ( formulaExp : Expression ) : 4D.Function

ParâmetroTipoDescrição
formulaExpExpression->Fórmula a ser retornada como objeto
Resultados4D. Function<-Função nativa encapsulando a fórmula

Descrição

The Formula command creates a 4D Function object based upon the formulaExp expression. formulaExp can be as simple as a single value or complex, such as a project method with parameters.

Ter uma fórmula como se fosse um objeto permite que seja passada como um parâmetro (atributo calculado) para comandos ou métodos, ou para ser executado a partir de vários componentes, sem precisar declará-los como "partilhados por componentes e database host". Quando chamado, o objeto fórmula é avaliado sem o contexto do banco de dados ou componente que o criou.

A fórmula retornada pode ser chamada com:

 var $f : 4D. Function
$f:=Formula(1+2)
$o:=New object("myFormula";$f)

//três formas diferentes de chamar a fórmula
$f.call($o) //retorna 3
$f.apply($o) //retorna 3
$o.myFormula() //retorna 3

You can pass parameters to the Formula, as seen below in example 4.

You can specify the object on which the formula is executed, as seen 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. Quando executados, a fórmula usa esses valores copiados ao invés do valor atual da variável local. Note que usar arrays como variáveis locais não são compatíveis.

The object created by Formula can be saved, for example, in a database field or in a blob document.

Exemplo 1

Uma fórmula simples:

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

var $o : Object
$o:=New object("f";$f)

$result:=$o.f() // devoluções 3

Exemplo 2

Uma fórmula usando um método projeto com parâmetros:



$value:=10
$o:=New object("f";Formula($value))
$value:=20

$result:=$o.f() // returns 10

Exemplo 3

Uma fórmula usando variáveis locais:

 $o:=New object("f";Formula($1+$2))
$result:=$o.f(10;20) //retorna 30

Exemplo

Uma fórmula simples usando parâmetros:

 $o:=New object("f";Formula(myMethod))
$result:=$o.f("param1";"param2") // equivalent to $result:=myMethod("param1";"param2")

Exemplo 2

Using This:

 $o:=New object("fullName";Formula(This.firstName+" "+This.lastName))
$o.firstName:="John"
$o.lastName:="Smith"
$result:=$o.fullName() //retorna "John Smith"

Exemplo 6

Chamar uma fórmula usando notação de objeto:

 var $feta; $robot : Object
var $calc : 4D.Function
$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]"}

Formula from string

História
ReleaseMudanças
20 R3Support of context parameter
17 R6Renamed New formula from string -> Formula from string
17 R3Adicionado

Formula from string( formulaString : Text ) : 4D.Function
Formula from string( formulaString : Text ; context : Longint ) : 4D.Function

ParâmetroTipoDescrição
formulaStringText->Fórmula texto a ser retornada como objeto
contextNumber->sk execute in current database (default) or sk execute in host database
Resultados4D. Function<-Objeto nativo encapsulando a fórmula

Descrição

The Formula from string command creates a 4D.Function object based upon the formulaString and, optionnally, a context. formulaString can be as simple as a single value or complex, such as a project method with parameters.

This command is similar to Formula, except that it handles a text-based formula and allows to define an execution context. It is usually recommended to use the Formula command, except if the original formula was expressed as text (e.g., stored externally in a JSON file), or if you want to create a formula in a host database while calling Formula from string from a component. É altamente recomendável usar a sintaxe com tokens com esse comando.

Because local variable contents can not be accessed by name in compiled mode, they can not be used in formulaString. An attempt to access a local variable with Formula from string will result in an error (-10737).

If the formula is created in a component, you might consider using the context parameter. Por padrão, dado que as fórmulas são executadas no contexto em que foram criadas, não conseguirá chamar uma variável, uma função ou um método não compartilhado do banco de dados host. In this case, you can pass the sk execute in host database constant in the context parameter to execute the 4D.Function object in the context of the host database. Estão disponíveis as seguintes constantes:

ParâmetrosTipoDescrição
sk execute in current databaseLongint(padrão) A fórmula será executada no contexto em que foi criada
sk execute in host databaseLongintA fórmula será executada no contexto do banco de dados do host

Exemplo

O código abaixo cria um diálogo aceitando uma fórmula em formato texto:

 var $textFormula : Text
var $f : 4D.Function
$textFormula:=Request("Please type a formula")
If(ok=1)
$f:=Formula from string($textFormula)
ALERT("Result = "+String($f.call()))
End if

...e executa a fórmula:

.apply()

História
ReleaseMudanças
17 R3Adicionado

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

ParâmetroTipoDescrição
thisObjObject->Objeto a ser retornado pelo comando This na fórmula
formulaParamsCollection->Collection of values to be passed as $1...$n when formula is executed
Resultadosany<-Valores de execução de fórmula

Descrição

The .apply() function executes the formula object to which it is applied and returns the resulting value. The formula object can be created using the Formula or Formula from string commands.

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

You can also pass a collection to be used as $1...$n parameters in the formula using the optional formulaParams parameter.

Note that .apply() is similar to .call() except that parameters are passed as a collection. Isso pode ser útil para passar resultados calculados.

Exemplo 1

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

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

Exemplo 2

 var $calc : 4D.Function
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
17 R3Adicionado

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

ParâmetroTipoDescrição
thisObjObject->Objeto a ser retornado pelo comando This na fórmula
paramsany->Valores a serem passados como $1...$n quando a fórmula for executada
Resultadosany<-Valores de execução de fórmula

Descrição

The .call() function executes the formula object to which it is applied and returns the resulting value. The formula object can be created using the Formula or Formula from string commands.

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

You can also pass values to be used as $1...$n parameters in the formula using the optional params parameter(s).

Note that .call() is similar to .apply() except that parameters are passed directly.

Exemplo 1

 var $f : 4D.Function
$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) // returns 100

.source

História
ReleaseMudanças
18 R2Adicionado

.source : Text

Descrição

The .source property contains the source expression of the formula as text.

This property is read-only.

Exemplo

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