Functions and Directives

Function Eval() Foundation

Executes the program code contained in a code block.

Syntax
Eval( <bBlock>, [<ExpressionList,...>]) --> xLastValue
Parameters
<bBlock>
<bBlock> is a code block whose program code is executed.
<ExpressionList,...>
<ExpressionList,...> is a list of expressions whose values are passed to the code block as parameters.
Return

The return value of Eval() is the value of the last expression in the program code of <bBlock>. Eval() can thus return a value with any data type.

Description

The code block function Eval() executes the program code within a code block. A code block is a value which references a piece of compiled program code and can be assigned to a variable. The code block is generated at a separate time from when the code it referecens it is executed. Code blocks represent a powerful programming tool and Eval() is one of the most powerful functions in Xbase++.

A code block references a piece of program code. This program code is executed when the code block is passed to the function Eval(). When more than one argument is included (<ExpressionList,...>), Eval() passes all subsequent arguments on to the code block. The program code in a code block can consist of a list of expressions separated by commas. The value of the last expression in the list is the return value of the code block and is also returned by the function Eval().

Generally, the program code within a code block is generated at compile time. However, code blocks can be generated at runtime by executing the macro operator (&) to compile a character string having the correct code block syntax.

Using Xbase++, the contents of code blocks can be stored (code blocks can be persistent). In this way generic data driven applications can be written whose program code is partly stored in files and then read and compiled during runtime of a program.

The iteration functions AScan(), AEval(), ASort() and DbEval() each process a code block which is evaluated for each element of an array or database.

Examples
Eval()
// In this example, various code blocks are created and 
// executed with Eval(). 

PROCEDURE Main 
   LOCAL bBlock 

   bBlock := {|n| n + 1 } 
   ? Eval(bBlock, 1)                // result: 2 

   bBlock := {|n1,n2| n1 + n2 } 
   ? Eval(bBlock, 5, 10 )           // result: 15 

   bBlock := {|n| IIf(n<0,"Negative","Positive") } 
   ? Eval(bBlock, -1 )              // result: Negative 

RETURN 

Save environment variables

// In this example, the function UserFunc() saves 
// the cursor position before output of a string 
// on the screen and restores it with Eval() after 
// the output. 

PROCEDURE Main 
   LOCAL nRow, nCol 
   CLS 
   nRow := Row() 
   nCol := Col() 

   ?? nRow, nCol                    // result: 0, 0 
   SetPos( nRow, nCol ) 

   UserFunc( "Test for cursor position" ) 

   ?? Row(), Col()                  // result: 0, 0 
RETURN 


FUNCTION UserFunc( cString ) 
   LOCAL aArray := ; 
   {  Row() , ;                     // current cursor row 
      Col() , ;                     // current cursor column 
      {|n1,n2| SetPos(n1,n2) } ;    // store call to SetPos() 
   }                                // in the code block 

   SetPos( 10, 15 ) 
   ?? cString 
   ?? Row(), Col()                  // result: 10, 39 

                                    // reset cursor 
   Eval( aArray[3], aArray[1], aArray[2] ) 

RETURN NIL 

Create a code block with the macro operator

// The example shows how character strings can be 
// compiled into a code block using the macro operator. 
// Character strings with code block syntax are stored 
// in an array. These character strings are compiled 
// with & into a code block to which another array 
// is passed. 

PROCEDURE Main 
   LOCAL aArray := {"One","Two","Three"} 
   LOCAL aBlock[3], i 

   FOR i:=1 TO 3 
      aBlock[i] := "{|a| a["+ Str(i,1) +"] }" 
   NEXT 

   ? aBlock[1]                       // result: {|a| a[1] } 
   ? Eval( &( aBlock[1] ), aArray )  // result: One 

   ? aBlock[2]                       // result: {|a| a[2] } 
   ? Eval( &( aBlock[2] ), aArray )  // result: Two 

   ? aBlock[3]                       // result: {|a| a[3] } 
   ? Eval( &( aBlock[3] ), aArray )  // result: Three 

RETURN 

Control the return value of a code block

// In this example the return value of a code block is determined 
// by the parameter which is passed to the code block: 

PROCEDURE Main 
   LOCAL aArray := { "One", 100 , Date() } 
   LOCAL bBlock := {|a,n| a[n] } 

   ? Eval( bBlock, aArray, 1 )      // result: One 
   ? Eval( bBlock, aArray, 2 )      // result: 100 
   ? Eval( bBlock, aArray, 3 )      // result: 12/06/94 

RETURN 
Feedback

If you see anything in the documentation that is not correct, does not match your experience with the particular feature or requires further clarification, please use this form to report a documentation issue.