Have an amazing solution built in RAD Studio? Let us know. Looking for discounts? Visit our Special Offers page!
News

Using Delphi’s Expressions Engine

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

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.

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:

Not that its much useful, as the only predefined operations for strings is concatenation, so you can type in the input:

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):

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):

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:

would result in John is happy. But I can also use the expression:

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:

To invoke these methods, you need to bind an object of this type:

Now you can write expressions like:

resulting in: At double age John will be 66 years old

The Demo UI

Not that fancy, but this is the demo in action:

bindingexpressions-6003371

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.

rad-studio-banner-blog


What's New for RAD Studio 11

Reduce development time and get to market faster with RAD Studio, Delphi, or C++Builder.
Design. Code. Compile. Deploy.
Start Free Trial   Upgrade Today

   Free Delphi Community Edition   Free C++Builder Community Edition

About author

Marco is one of the RAD Studio Product Managers, focused on Delphi. He's the best selling author of over 20 books on Delphi.

1 Comment

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

IN THE ARTICLES