Binding variables
Sometimes it is desirable to provide the user with variables (though the word "variable" is slightly misleading. From a user's point of view, they are more like constants). For example, a Forex software might want to expose current foreign exchange rates on the fly. To do so, simply call one of the Evaluator.map()
functions, e.g.
Evaluator e = new Evaluator();
e.map("dollar",1.43);
// Or more conveniently:
String raw = "yen = 0,008";
e.mapFromString(raw);
Passing null instead of a number will clear the respective variable (if set). A few things to keep in mind:
- It is the application developers responsibility to validate names. Mapping illegal names is not an error. They will just not be accessible for the user (this can actually be desirable in some cases, e.g. when implementing custom functions that require "internal" variables).
- The
Evaluator
will always create a fresh copy of it's variable memory whenever a new expression is evaluated. This means, it is not possible to hand over results from one expression to the other.