Skip to main content
Version: 1.0.0

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. QodlyScript 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 formula.

formula objects

The formula and formulaFromString commands allow you to create formula functions, i.e. 4D.Function objects to execute any expression or code expressed as text.

formula objects can be encapsulated in object properties:

 var f : 4D.Function
f = newObject
f.comp = formula(1+2)

This property is an "object function", i.e. a function which is bound to its parent object. To execute a function stored in an object property, use the () operator after the property name, such as:

 f.comp() //returns 3

Syntax with brackets is also supported:

 f["comp"]() //returns 3

Note that, even if it does not have parameters (see below), an object function to be executed must be called with () parenthesis. Calling only the object property will return a new reference to the formula (and will not execute it):

 o = f.comp //returns the formula object in o

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

 f.comp.apply() //returns 3

Passing parameters

You can pass parameters to your formulas using sequentially numbered "$" variables: $1, $2, $3, and so on. The numbering of the variables represents the order of the parameters.

For example, you can write:


var f : object
f = newObject
f.comp = formula(1+$1)
f.comp(5) //returns 6

Or using the .call() function:

 var f : 4D.Function
var r : integer
f = formula($1+$2)
r = f.call(null,5,5) //r: 10
r = f.call(null,10,yearOf(currentDate)) //r: 2033

Parameters to a single method

For more convenience, when the formula is made of a single method, parameters can be omitted in the formula object initialization. They can just be passed when the formula is called. For example:

 var f : 4D.Function
var t : string

f = formula(myMethod)
//Writing formula(myMethod($1,$2)) is not necessary
t = f.call(null,"Hello","World") //returns "Hello World"
t = f.call() //returns "How are you?"

//myMethod
declare (param1 : string, param2 : string)->return : string
if(countParameters = 2)
return = param1+" "+param2
else
return = "How are you?"
end

Parameters are received within the method, in the order they are specified in the call.

Functions and properties

    
    
    

.apply()

.apply*() : any
.apply( thisObj : object { , formulaParams : collection } ) : any

ParameterTypeDescription
thisObjobjectObject to be returned by this in the formula
formulaParamscollectionCollection of values to be passed as $1...$n when formula is executed
ResultanyValue from formula execution

Description

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 formulaFromString 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. This can be useful for passing calculated results.

Example 1

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

c = newCollection(10,20,30)
result = f.apply(null,c) // returns 60

Example 2

 var calc : 4D.Function
var feta, robot : object
robot = newObject("name","Robot","price",543,"quantity",2)
feta = newObject("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()

.call*() : any
.call( thisObj : object { , ...params : any } ) : any

ParameterTypeDescription
thisObjobjectobject to be returned by this in the formula
paramsanyValue(s) to be passed as $1...$n when formula is executed
ResultanyValue from formula execution

Description

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

Example 1

 var f : 4D.Function
var result : string
f = formula(uppercase($1))
result = f.call(null,"hello") // returns "HELLO"

Example 2

 var f : 4D.Function
var o : object
var result : integer
o = newObject("value",50)
f = formula(this.value*2)
result = f.call(o) // returns 100

.source

.source* : string

Description

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

This property is read-only.

Example

 var of : 4D.Function
var tf : string
of = formula(string(currentTime,HH MM AM PM))
tf = of.source //"string(currentTime,HH MM AM PM)"