Home

TOTVS Technology

Child pages
  • Code Blocks
Skip to end of metadata
Go to start of metadata

Code Blocks are a powerful AdvPL resource. They enable the export of small pieces of executable code from one place to another in the application. AdvPL considers code blocks as data values, enabling them to the stored in variables, entered as arguments for functions and used as a more efficient alternative to the macro operator ( & ).

 

Definition

The syntax used for a code block definition is:

 

{ | [ <lista de argumentos> ] | <lista de expressões> }

 

The arguments ( <lista de argumentos> ) and the expressions( <lista de expressões> ) must be separated by commas. The expressions specified cannot have commands, control structures of programming flow (WHILE ... ENDDO, IF ... ENDIF, etc.) or declarations (LOCAL, PRIVATE, etc.).

 

The vertical slashes ( | | ), that establish the code blocks are mandatory, even if there are no arguments. Hereinafter some code blocks examples:

{ | | “Bloco sem argumentos” }
{ | x | x + 1 }
{ | a, b | Func1(a) + Func2(b) }
{ | i, j, k | Func(i), Func(j), Func(k) }

 

As mentioned before, a code block can be attributed to a variable or an array element through the operator in line ( := ).

 

Code Blocks Run

The run or evaluation of the code blocks result is performed by function  EVAL(), whose syntax is:

 

EVAL( <bloco de código>, <lista de parâmetros> )

 

The function EVAL() run the specified code block ( <bloco de código> ), transferring the arguments in the parameters list ( <lista de parâmetros> ) . The expressions in the code block are run and evaluated one by one from left to right. The code block result value is the last expression result. For example:

bCodBloc := { | nNum1, nNum2 | nNum1 * nNum2 }
MSGALERT( EVAL(bCodBloc, 2, 3) )

 

The result of the expression above is 6.

Besides the function EVAL(), the functions AEVAL() and DBEVAL() also run code blocks. These functions respectively process arrays and data files, running the specified code block for each array elements or data file record.

 

Code Blocks Use

The code blocks are particularly used in the implementation of flexible routines, that is, that can be run irrespective of the type of data transferred to them.

The example below shows the function Message without the code blocks use. In this case, the function depends on the type of data transferred.

 

// It displays, for a determined amount of times, a data on the screen.
FUNCTION Message(xDado, nVezes)
	LOCAL nRepet := 0

	IF VALTYPE(xDado) == "C"
	// Character type data
		xDado := xDado

	ELSEIF VALTYPE(xDado) == "N"
	// Numeric type data
		xDado := LTRIM(STR(xDado))

	ELSEIF VALTYPE(xDado) == "D"
	// Date type data
		xDado := DTOC(xDado)

	ELSEIF VALTYPE(xDado) == "L"
	// Logic type data
		xDado := IIF(xDado, "Verdadeiro", "Falso")

	ELSE
	// Another type of data
		xDado := "Indefinido"
	ENDIF

	FOR nRepet := 1 TO nVezes
		MSGALERT(xDado)
	NEXT nRepet

	RETURN

 

To make function Message more generic, irrespective of the type of data transferred:

bCarCod   := { | xDado | xDado }
bNumCod   := { | xDado | LTRIM(STR(xDado)) }
bLogCod   := { | xDado | IF(xDado, “Verdadeiro”, “Falso”) }
bDatCod   := { | xDado | DTOC(xDado) }

 

Transferring the code blocks defined above, the function Message could have the following format:

// It displays, for a determined amount of times, a data on the screen.
FUNCTION Message(xTexto, bBloco, nVezes)

	LOCAL nPesoSac := 0

	FOR nRepet := 1 TO nVezes
		MSGALERT(EVAL(bBloco, xTexto))
	NEXT nRepet

	RETURN

 

Therefore, one of the code blocks basic usages that generically consider data, making them more flexible and independent, as they are run in compliance with the code block transferred.

The code blocks usage is an important  AdvPL language resource. As you get familiar with this resource, it becomes simpler, and user friendly. When it is well used and understood, the code block makes the source code more generic, flexible and modular, increasing productivity in the maintenance and in new programs developing.

 

Code Blocks Maintenance

Code blocks cannot be stored straight in data file fields, unless they are transformed in character type variables. To use them later, the macro operator is utilized as follows:

FUNCTION Message()

	LOCAL bBloco := ""

	// It saves the code block in the field of Table SE2 (Payable Bill) as a character variable.
	SE2->E2_TEXTO := " { |xDado| Func1(xDado) } "

	// Attributes the code block to the variable bBloco
	bBloco := &(SE2->E2_TEXTO)

	// Runs the code block stored in the variable bBloco
	EVAL(bBloco, xDado)

	RETURN NIL
  • No labels