Delphi RTL include a very powerful expression engine, which is one of the foundations of the Live Bindings architecture, but can be used as a separate engine for processing expressions. This blog post offers a quick introduction to the topic.
There are many hidden gems in the Delphi RTL. One of them is the expression engine. Recently I had some conversations with a long time Delphi developer who was looking for a similar feature, not realizing it has been in the product for many years. I knew I had written some demos and documentation so I spent a little time searching for them and I found a few. Now this topic is very complex and I won’t be able to cover it in full, but for simple scenarios you really need very little code to parse and process an expression.
Before I start with the real topic I also wanted to mention we have recently surfaced this feature in the new VCL NumberBox component (added in Delphi 10.4.2, see for example https://blog.marcocantu.com/blog/2021-february-new-vcl-controls-1042.html). This component allows end users to enter an expression and replace it with the resulting value. No surprise it used the existing expression engine and it does by invoking a simplified class method of
[crayon-676877bc4a631697974342/]
In this code snippet LText is a string with the expression and Value is the floating point result. The TBindings.CreateExpression class method is a shortcut that can simplify the code, but I’d rather guide you to some more of the details.
Table of Contents
Key Concepts of Binding Expressions
The code above creates a TBindingExpression object, the core class of this engine. As the name implies, this goes beyond being a pure expression evaluator, but can “bind” or associate the expression to external objects, using RTTI for integration.
Another key concept of binding expressions is they don’t evaluate the input in a completely dynamic way, but they rather require a parsing operation (called Compile) that processes the text and an evaluate operation that does the final processing. Of course, if you don’t change the expression text, you can compile once and evaluate the expression multiple times with different values for the associated objects.
Let’s Get to a Demo
For my first demo, I’ve create form with two Memo controls, one for typing the expression and the second to display the output. The goal here is to process strings, not numeric values. The code for the only button uses the binding expression objects directly, as follows:
[crayon-676877bc4a639860267203/]
Not that its much useful, as the only predefined operations for strings is concatenation, so you can type in the input:
[crayon-676877bc4a63b265789458/]
and get Hello world as result. Notice the double quotes in the input!
Binding an Object
Where things start getting interesting is if you bind the expression to an object. For this, I’ve created a simple class like the following (here I’ve omitted the private fields and methods):
[crayon-676877bc4a63d086646714/]
Now I can change the code to add a binding to the expression, adding to the Compile method the association of an object with a symbolic name (you can consider it like the name of the object for the expression engine):
[crayon-676877bc4a63f605307252/]
With this extension, I can now use this object in an expression like. For example, after assigning ‘John’ to the Name of the pers object in code, the expression:
[crayon-676877bc4a641135215220/]
would result in John is happy. But I can also use the expression:
[crayon-676877bc4a642114022136/]
which is going to do the type transformation from Integer to string.
Binding Functions
Binding expressions are limited to objects, but beside accessing to properties they can also execute methods. Therefore you can create a class with several methods to be used in an expression:
[crayon-676877bc4a643376374387/]
To invoke these methods, you need to bind an object of this type:
[crayon-676877bc4a644764654488/]
Now you can write expressions like:
[crayon-676877bc4a646855152555/]
resulting in: At double age John will be 66 years old
The Demo UI
Not that fancy, but this is the demo in action:
Only Touching the Surface
This introduction is only touching the surface, as binding expression allow binding controls and also allows registering for notifications (a sort of call back mechanism). I have found a few more demos I’ll try to blog about them soon.