Usina BRAdvPL Guide
Sign in
← All topics
FOUNDATIONPublished

Code blocks (CodeBlock)

{ |[parâmetros]| [expressão1, expressão2, ...] }

Create storable anonymous functions, receive parameters and evaluate expressions on demand with Eval().

CodeBlockCode blocksEvalAnonymous functionsExpressions
01 · OVERVIEW

Overview

A code block is an AdvPL value of type B (Block), similar to an anonymous function. It may receive parameters and execute one or more comma-separated expressions. Eval() executes the block and returns the result of its last expression. Unlike a regular function, its body accepts expressions and function calls, but not language commands.

02 · SYNTAX

Syntax

{ |[parâmetros]| [expressão1, expressão2, ...] }

Parameters

parâmetros
ListaOptional

Names between the vertical bars. They are local to the block and receive the supplied arguments.

expressões
AdvPLOptional

One or more comma-separated AdvPL expressions, executed from left to right.

retorno
QualquerOptional

The result of the last expression; an empty block returns NIL.

03 · PRACTICAL EXAMPLE

Simple block with a return value

User Function ExBlockSum()
    Local bAdd := { |nValue1, nValue2| Abs(nValue1) + Abs(nValue2) }
    Local nResult := Eval(bAdd, 10, -20)

    ConOut("Result: " + CValToChar(nResult))
Return
Expected result

Eval() supplies two arguments and receives 30, the result of the last expression.

04 · PRACTICAL EXAMPLE

Multiple expressions

User Function ExBlockList()
    Local bCalculate := { |nValue| ConOut("Received: " + CValToChar(nValue)), nValue * 2 }
    Local nResult := Eval(bCalculate, 5)

    MsgInfo("Double: " + CValToChar(nResult))
Return
Expected result

Expressions run from left to right and the return value is 10, the result of the last expression.

05 · PRACTICAL EXAMPLE

Custom sorting with ASort

User Function ExOrdenacao()
    Local aNomes := {"CARLA", "ANDREA", "BEATRIZ"}
    Local bDecrescente := { |cNome1, cNome2| cNome1 > cNome2 }

    ASort(aNomes, , , bDecrescente)
Return
Expected result

The block compares pairs selected by ASort() and enables descending order.

06 · PRACTICAL EXAMPLE

Pass by reference

User Function ExReferencia()
    Local bIncrementar := { |nValor| nValor += 1 }
    Local nContador := 0

    Eval(bIncrementar, nContador)  // nContador continua 0
    Eval(bIncrementar, @nContador) // nContador passa a 1
Return
Expected result

Without @, the block receives a copy; with @, its change reaches the original variable.

07 · PRACTICAL EXAMPLE

Capturing a local variable

User Function ExContexto()
    Local nContador := 0
    Local bSomar := { |nValor| nContador += nValor }

    Eval(bSomar, 5)
    ConOut("Contador: " + CValToChar(nContador))
Return
Expected result

The block references and changes nCounter in the Local context where it was created.

BEST PRACTICES
  • Braces delimit the block and two vertical bars delimit its parameter list, even when empty.
  • Block parameters have scope restricted to the block itself.
  • Eval(bBlock, arguments...) executes the content; @ passes a variable by reference.
  • Blocks are used for visual component events, sorting, evaluations and other APIs that receive behavior as an argument.
  • A block may capture a Local variable visible when it is created and retain that context while referenced.
COMMON PITFALLS
  • { .T. } and { IIF(...) } are one-element arrays, not code blocks; they lack vertical bars.
  • Avoid large blocks: extract the logic into a function and let the block call it.
  • The official reference advises against calling a Static Function inside a block that may run from another source file because duplicate static names may cause indeterminate behavior.
  • Keeping a block that captured Local context for a long time may retain its memory area; remove references with NIL when no longer needed.
  • Follow the parameter count and return type expected by the function that evaluates the block.

Related content

REFERENCES
  1. TOTVS. Code Blocks. TDN. Created by Adriana Panseri Santos; last changed by Julio Wittwer on September 11, 2021.
Status
Published
Page created on
Last reviewed on
Original language
Portuguese
Reviewed by
Usina.BR