Usina BRAdvPL Guide
Sign in
← All topics
FOUNDATIONPublished

Data typing in AdvPL

Local <variável> As <tipo> · Function <nome>(<parâmetro> As <tipo>) As <tipo>

Declare variable, parameter, return, property and method types to make inconsistencies more visible.

Data typesVariablesFunctionsClassesCompilation
01 · OVERVIEW

Overview

AdvPL is dynamically typed, but explicit types may be declared in .PRW source files. These annotations help the compiler and AppServer identify incompatible assignments, parameters and return values. Documented types are numeric, character, date, codeblock, logical, array, object and variadic. TLPP typing has its own features and documentation.

02 · SYNTAX

Syntax

Local <variável> As <tipo> · Function <nome>(<parâmetro> As <tipo>) As <tipo>

Parameters

numeric
NOptional

Positive or negative integer and decimal numeric values.

character / char
COptional

Character strings.

date
DOptional

Date values.

codeblock / block
BOptional

Code blocks that can be evaluated at runtime.

logical
LOptional

True (.T.) or false (.F.) logical values.

array
AOptional

Arrays that can contain values and structures.

object
OOptional

Class instances or interface objects.

variadic
HOptional

A variable number of function parameters.

03 · PRACTICAL EXAMPLE

Typed local variables

User Function ExTypes()
    Local cMessage As Character
    Local nQuantity As Numeric
    Local dToday As Date
    Local lActive As Logical
    Local aItems As Array

    cMessage  := "Registration available"
    nQuantity := 3
    dToday    := Date()
    lActive   := .T.
    aItems    := {"A", "B", "C"}

    If lActive
        MsgInfo(cMessage + " - items: " + CValToChar(nQuantity))
    EndIf
Return
Expected result

Each variable declares its expected type, making incompatible assignments easier to identify.

04 · PRACTICAL EXAMPLE

Typed parameter and return

User Function OrderTotal(nValue As Numeric, nQuantity As Numeric) As Numeric
    Local nTotal As Numeric

    nTotal := nValue * nQuantity
Return nTotal
Expected result

The prototype documents that both parameters and the return value are numeric.

05 · PRACTICAL EXAMPLE

Class property and method

Class Pedido
    Data nTotal As Numeric
    Method New(nValor As Numeric) As Object
EndClass

Method New(nValor As Numeric) Class Pedido
    ::nTotal := nValor
Return Self
Expected result

The property, parameter and constructor return have declared types.

06 · PRACTICAL EXAMPLE

Detectable incompatibility

User Function ExIncompativel()
    Local cCodigo As Character
    Local nTotal  As Numeric

    nTotal  := "ABC"
    cCodigo := 10
Return
Expected result

The compiler may issue warning W0015 for incompatible C and N conversions. Correct the source or convert the value deliberately.

BEST PRACTICES
  • Use As <type> on local variables, parameters, function returns, class properties and methods.
  • Explicit typing improves routine contracts and allows the compiler to report incompatibilities.
  • For typed calls, AppServer also compares the received type with the defined prototype.
  • The official reference lists historical requirements from December 2014; confirm behavior in the version in use.
COMMON PITFALLS
  • Do not ignore compilation warnings: assigning character to numeric or vice versa conflicts with the declared contract.
  • According to the documentation, an incompatible argument may reach the routine as NIL, produce a console warning and alter the expected flow.
  • Do not confuse .PRW typing with TLPP (.tlpp) typing, which has separate features and documentation.
  • Explicit typing does not replace validation of NIL, content, ranges and business rules.

Related content

REFERENCES
  1. TOTVS. Data Typing. TDN. Created by Thays Tagliaferri de Grazia; last changed by Cristiano Lino Felicio on August 25, 2021.
Status
Published
Page created on
Last reviewed on
Original language
Portuguese
Reviewed by
Usina.BR