xHarbour Reference Documentation > Operator Reference xHarbour Developers Network  

{|| }

Literal code block.

Syntax

{|[<params,...>]| <expressions,...> }

Arguments

<params,...>
This is an optional comma separated list of parameter names declared for use inside the code block. Code block parameters are only visible within the code block and must be placed between the || delimiters.
<expressions,...>
<expressions,...> is a list of comma separated expressions to execute in the code block. The return value of the code block is the return value of the last expression in the list.

Description

Literal code blocks are created in program code using the {|| } characters. A code block is an extremely powerful instrument since it allows a programmer to separate the definition of program code from its execution time. A code block is a piece of executable code that can be assigned to a variable. This makes a code block similar to a macro expression but in contrast to a macro, a code block is resolved at compile time. This makes code blocks considerably faster than macro expressions that are compiled at runtime.

The program code, embedded within a 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 in the code block are then executed from left to right. The return value of the last expression is finally returned from the code block.

When LOCAL variables exist at the time of code block creation and are included in the expression list, they are embedded in the code block. This has a far reaching consequence: normally, LOCAL variables cease to exist when the function returns in which the LOCALs are declared. If, however, the function returns a code block with embedded LOCAL variables, they continue to exist inside the code block and for the lifetime of it. This way, LOCAL variables can become detached from the declaring function context.

Info

See also:<|| >, AEval(), AScan(), ASort(), DbEval(), Eval(), HEval(), LOCAL
Category: Indirect execution , Operators , Special operators
LIB:xhb.lib
DLL:xhbdll.dll

Example

// The example demonstrates two basic techniques for using code blocks.
// The first code block receives parameters from outside (the Eval()
// function), while the second code block uses embedded LOCAL variables
// that are detached from function MyDispbox().

   PROCEDURE Main
      LOCAL nTop   :=  5
      LOCAL nLeft  := 10
      LOCAL nBot   := 20
      LOCAL nRight := 70
      LOCAL bBlock := {|t,l,b,r| DispBox(t,l,b,r) }

      CLS
      // draw a box at screen
      Eval( bBlock, nTop, nLeft, nBot, nRight )

      @ MaxRow()-1, 0
      WAIT "press key to clear screen"

      CLS
      @ MaxRow()-1, 0
      WAIT "press key to redraw box"

      bBlock := MyDispBox( nTop, nLeft, nBot, nRight )

      @ MaxRow()-1, 0
      WAIT "press key to restore screen"

      Eval( bBlock )

      @ MaxRow()-1, 0
      WAIT "press key to end           "
   RETURN

   // this function creates a code block with detached LOCALs
   FUNCTION MyDispBox( nT, nL, nB, nR )
      LOCAL cScreen := SaveScreen( nT, nL, nB, nR )

      Dispbox(  nT, nL, nB, nR )
   RETURN {|| RestScreen( nT, nL, nB, nR, cScreen ) }

Copyright © 2006-2007 xHarbour.com Inc. All rights reserved.
http://www.xHarbour.com
Created by docmaker.exe