Have you ever needed to validate and execute dynamic business rules at runtime in your Uniface applications? The proccompile
statement is your secret weapon! ๐ช
This article is based on the Uniface Documentation 10.4, and I had assistance from AI in crafting this content.
๐ฏ What is proccompile?
The proccompile
statement allows you to compile ProcScript code dynamically at runtime, either as an expression or condition. This powerful feature enables you to validate user-entered business rules before executing them, preventing runtime errors and creating more robust applications.
๐ Syntax Overview
proccompile /expression | /condition ProcScript {, ContextList}
Qualifiers
- /expression - Compiles the ProcScript as an expression ๐งฎ
- /condition - Compiles the ProcScript as a condition โ
Parameters
- ProcScript (String) - The ProcScript code to be compiled
- ContextList (Optional) - Nested list defining available objects like FIELDS, VARIABLES, ITEMS, etc.
๐ Return Values and Status
After execution, proccompile
returns:
- $result - Contains the compiled ProcScript module
- $status = 0 - Successful compilation โ
- $status < 0 - Compilation error occurred โ
When errors occur, $procerror
contains detailed error information, and $abs($status)
shows the position where the error occurred.
๐ก Real-World Example: Dynamic Discount Calculator
Let's explore a practical scenario where users can define custom discount rules at runtime:
function total_cost
variables
numeric vDiscount
endvariables
; Validate discount condition syntax
proccompile/condition DO_DISCOUNT, "FIELDS=AMOUNT"
if ($procerror < 0)
message/error "Invalid discount condition: %%$procerror"
return -1
endif
; Check if discount applies
if ($condition($result))
; Validate discount expression
proccompile/expression DISCOUNT, "FIELDS=AMOUNT!;PRICE"
if ($procerror < 0)
message/error "Invalid discount expression: %%$procerror"
return -1
endif
; Calculate discount
vDiscount = $expression($result)
else
vDiscount = 0
endif
COST = AMOUNT * PRICE - vDiscount
return 0
end
How it Works ๐ ๏ธ
In this example:
- DO_DISCOUNT field might contain:
AMOUNT>100
- DISCOUNT field might contain:
0.1*AMOUNT*PRICE
- The code validates both expressions before execution
- If valid, it applies the business logic dynamically
๐จ Best Practices
- Always validate first - Use
proccompile
before executing dynamic code ๐ - Handle errors gracefully - Check
$procerror
and provide meaningful messages ๐ฌ - Define context properly - Specify available fields and variables in ContextList ๐
- Cache compiled results - Store
$result
for repeated use if needed โก
๐ Why Use proccompile?
- Safety - Catch syntax errors before execution
- Flexibility - Enable user-configurable business rules
- Performance - Compile once, execute multiple times
- Debugging - Get precise error locations and descriptions
๐ Conclusion
The proccompile
statement transforms static Uniface applications into dynamic, user-configurable systems. By validating and compiling ProcScript at runtime, you can create more flexible and robust business applications that adapt to changing requirements without code modifications.
Start experimenting with proccompile
in your next Uniface project - your users will appreciate the flexibility! ๐
Top comments (0)