xHarbour Reference Documentation > Operator Reference |
Extended literal code block.
<| [<params,...>] | <programcode> >
Extended literal code blocks are created in program code using the <|| > characters. They can be used in the same way as regular code blocks but have the advantage that <programcode> can include any statements allowed in the body of a function. This includes statements spanning across multiple lines, such as loops (DO WHILE, FOR and FOR EACH), branching (DO CASE and SWITCH), error handling (BEGIN SEQUENCE and TRY...CATCH).
Even the declaration of LOCAL variables within an extended code block is supported. Only STATIC variables cannot be declared.
The program code, embedded within an extended code block, is executed by passing the code block to the Eval() function. Arguments passed to this function are passed on to the code block and are received by the code block parameters. The expressions and statements in the code block are then executed from left to right, or top to bottom, respectively. The return value of the code block must be specified with the RETURN statement.
See also: | {|| }, AEval(), AScan(), ASort(), DbEval(), Eval(), HEval(), LOCAL |
Category: | Indirect execution , Operators , Special operators , xHarbour extensions |
LIB: | xhb.lib |
DLL: | xhbdll.dll |
// The example demonstrates the creation of an extended code block as a // return value of function ConversionBlock(). The code block converts // values to character strings. Note that the code block calls itself // recursively within the FOR EACH loop. PROCEDURE Main LOCAL bBlock, lLogic:= .T., nNumber:= 1.23456, aArray := Directory() bBlock := ConversionBlock() ? Eval( bBlock, aArray ) ? Eval( bBlock, lLogic ) ? Eval( bBlock, nNumber ) ? Eval( bBlock, GetNew() ) ? Eval( bBlock, bBLock ) RETURN FUNCTION ConversionBlock() LOCAL bBlock bBlock := <| xValue | LOCAL cType := Valtype( xValue ) LOCAL cValue, xElem SWITCH cType CASE "A" cValue := "{" FOR EACH xElem IN xValue cValue += Eval( bBlock, xElem ) + "," NEXT cValue[-1] := "}" EXIT CASE "B" ; cValue := "{||...}" ; EXIT CASE "C" ; cValue := xValue ; EXIT CASE "D" ; cValue := DtoS(xValue) ; EXIT CASE "L" ; cValue := IIf(xValue, ".T.",".F.") ; EXIT CASE "N" ; cValue := LTrim(Str(xValue)) ; EXIT CASE "O" ; cValue := xValue:className() ; EXIT DEFAULT cValue := "NIL" END RETURN cValue > RETURN bBlock
http://www.xHarbour.com