A new command is at your disposal allowing you to use formulas in your code. You can now encapsulate them in objects and call them when needed. There’s no more need to write your code as text. Now you can just pass your formula in your command and that’s it! This is a great addition, since your code isn’t text, you can benefit from Syntax highlighting and all the other advanced functionalities of the code editor!
Encapsulate a formula in an object
With the New formula command, it’s very simple to encapsulate a formula in an object:
$f:=New formula(ALERT("Hello world"))
And of course, because the formula is encapsulated in an object, you can encapsulate it in a property of an object and create your own encapsulated object methods:
$f.message:=New formula(ALERT("Hello world"))
$f.message() // Display alert dialog box with the message "Hello world"
If you want to pass parameters to your formula, just use $1, $2 … as in your methods:
$f.message:=New formula(ALERT("Hello "+$1))
// Display alert dialog box with the message "Hello John"
Or if you want to use the value of an object attribute, the This command is the answer:
// Display alert dialog box with the message "Hello world"
Finally, you can use a method as a formula:
$person.greeting:=New formula(Greeting )
// $g:="hello John Smith"
// $g:="hi John Smith"
With the Greeting method:
$0:=$1+" "+This.firstName+" "+This.lastName
You can find other examples of how to use New formula in this HDI:
Calling your formula
$f:=New formula(ALERT(String($1+" "+$2))
$f.apply(null;New collection("Hello";"world") //formula.apply object methods
Encapsulate a formula in an object from a string
There’s a way to use a text version of a formula to create a method with the New formula from string command:
$textFormula:=Request("Please type a formula")
$f:=New formula from string($textFormula)
ALERT("Result = "+String($f.call()))