The eval statement
Overview
Certain J services provide control functions on the J interpreter. The "eval" service is a powerful feature of an interpreted language.
Other services exist, which can help debug a J program, stop the execution of a J program or obtain information on the current execution context.
eval
The "eval"
service is used to calculate a character String dynamically, by considering its
contents as being composed of J instructions.
When the
String is evaluated, the current object does not change, it is the one in use
when the eval is called.
Note 1: Declarations
of local variables are authorized in the String to be evaluated.
Note 2: A return
statement in the evaluated String provokes a return from the method (non
anonymous) that contains the eval statement.
Example of eval
We are going to use "eval" to read the variable values previously stored in a file (Storage example):
Object:Storage();
{
outStream Fic;
Fic.open ("Example");
Fic.write ("i1 = 15; s1 = "hello";
b1=false");
Fic.close();
Recover();
} – End of Storage
Object:Recover();
{
String read _buffer;
inStream Fic;
int i1;
String s1;
boolean b1;
Fic.open ("Example");
Fic.read (read_buffer);
eval (read_buffer);
//at this
stage, the variables i1, s1, b1 are assigned.
StdOut.write
("i1=",i1,NL,
"s1=",s1,NL
"b1=",b1,NL);
}