|THE EVOLUTION OF CODE BLOCKS
|Tips and Tricks
Code blocks appeared for the first time in Clipper 5, but they did not just spring into existence -- there has been a steady progression towards them in the Clipper language.
Many concepts in Clipper are combined to implement code blocks. This document will attempt to show this in an easy, incremental manner.
First, A Reminder
Clipper is a language based on functions. Functions have return results. So, too, does the assignment operator
So, instead of writing:
X := 10 is evaluated first, then its result
(the value of
X, which is now
10) is passed to
? operator, which prints the value. Another way of
thinking about it is "The value 10 goes into X and then keeps on going
through to the print operator".
Another example is:
0 is stored into
X, and then it
keeps on going into
Y, and then into
Z. In fact,
it still keeps on going off the left-hand side of the
So if the code is changed to:
0 would appear on the screen.
You could say that the value
0 "propagated through the
expression", or "filtered through", or "bubbled up", or "kept on
Clipper allows you to join several lines of code together on one line. For example, this code:
Part 1: The Expression List
The evolution of code blocks starts with the expression list.
In the samples below the
==> symbol is meant to indicate
what you would see on the screen.
Two Lines Of Code
Start with two simple lines of code:
This is the same code as above, just written on one line.
Putting brackets around the code and replacing the semicolon with a comma makes it into an expression list:
So, this expression list evaluates
X := 10, then evaluates
Y := 20, then returns the last thing, which is then printed.
After this code is complete,
X will have a value of
Y will be
20 will appear on the screen.
Debugging expression lists is hard because they are typed on one line of source code, which means that all component expressions will have the same line number associated with them. This can make it difficult to determine where the error occurred.
Where Would you Use An Expression List?
The primary purpose of the expression list is to group things together into one unit. Anywhere in Clipper a single expression can be used, you can put an expression list. Thus, you can make several things happen where only one normally would.
For example, here is an
? is gone, replaced by the
function. This is because expression lists can only contain
? is a command, not an expression.
However, every command in Clipper has an equivalent function, so a
? in the manual indicated that
was the function to use.
This kind of code would (hopefully) not be used in a real program, but it is shown here to give you an idea of the mechanics of expression lists.
Part 2: From Expression List to Code Block
Starting With An Expression List
Here is the expression list again:
As mentioned earlier, an expression list is like a simple, one line function. Here is the function that is equivalent to the expression list above:
Here is where the magic happens. An expression list can be changed into a code block very easily.
You may have seen the
} braces before. That's
right -- arrays.
How Do You Print An Array Or A Code Block?
Since they are both pointers, they can not be printed out directly. Something more is needed. An array element is accessed by specifying and indexing number.
eval() To Evaluate A Code Block
A code block is evaluated (executed, run, triggered, fired, etc.) by using the
Part 3: Passing Parameters
If code blocks are like functions, then how are parameters passed to them?
First Look At The Function
Here is the sample function, modified to accept a parameter:
Here is the code block, modified to accept a parameter:
N appears, just like in the
Call The Function
Part 4: Using Code Blocks
Given the following array:
asort() function sorts in ascending order by default. That
behaviour can be changed by providing a code block that sorts in
The code block compares them and returns .T. if they are in the right order, or .F. if they are not. If the return value is .F., then ascan() swaps the two values in the array and moves on to the next pair of values. So, in the code block above, the comparison X > Y is .T. if the elements are in descending order, which means that the first value is greater than the second.
To sort on the last name, also in descending order:
After using this code block, the array now contains:
|Send comments about this site to Greg at
All pages copyright © 1996-1999 GH Services Created 1996/10/01 Last updated 1999/09/30
All trademarks contained herein are the property of their respective owners