Dear Peter, Raph, and all,

Peter writes:

I'm not sure exactly what you would like to do.  Here is some
information that might be useful for you.

As Raph deduced, I am looking to do dynamic checks of program
properties and raise an exception if it's not found.  I find this
especially useful for testing.

The Compiler.evalExpression function takes a string and an environment
and compiles and evaluates the string as an Oz expression.  See:

http://www.mozart-oz.org/documentation/compiler/node4.html#section.api.modul
e
For example, {Compiler.evalExpression "X+3" env('X':4) _} returns 7.

Thanks, I spent a while working with that, and even wrote up something
that evaluates an expression in the OPI's top-level environment, which
you can find at:

  http://www.cs.iastate.edu/~cs541/lib/OPIEval.oz

But in the end, I decided that this was too error prone, as the
top-level environment of the OPI is not always what is wanted, and
because this wouldn't make sense in a stand alone program.  So I have
gone back to the simplest thing:

   %% Assert that the argument is true.
   proc {Assert B}
      if {Not B}
      then raise assertionFailed(debug:unit) end
      end
   end

This certainly works, although in the OPI it is not ideal for
pinpointing where the error comes from, and the message one gets
doesn't display the source code for the expression that evaluated to
the argument (B).

For those interested, see

  http://www.cs.iastate.edu/~cs541/lib/Testing.oz

for a module with procedures assert, assume, and test.  All of these
are equally simple-minded, however.

My conclusion was that these simple versions were better than trying
to use the compiler's evalExpression procedure, because:

 - I couldn't figure out how to get the current lexical environment to
   pass to evalExpression as an argument, and
 - Quoting expressions is more error-prone using expressions directly.

Ideally assert would be a macro that could grab a quoted version of
the expression for use in the exception. (Or it could be directly
supported by the compiler.)

Thanks for all your help with this.

        Gary T. Leavens
        Department of Computer Science, Iowa State University
        229 Atanasoff Hall, Ames, Iowa 50011-1041 USA
        http://www.cs.iastate.edu/~leavens  phone: +1-515-294-1580


_________________________________________________________________________________
mozart-users mailing list                               
mozart-users@ps.uni-sb.de
http://www.mozart-oz.org/mailman/listinfo/mozart-users

Reply via email to