Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Expression Trees .net

I would like to get a good grasp on the subject, hence want to ask following questions to the community:

  1. What benefits do expression trees bring to table?
  2. What exactly does this statement mean: "Expression Trees are data compiled as code"?
  3. What common challenges do the Expression Trees and functional programming in the context of .net solve?
  4. What are good online resources to get up the speed on the subj?
like image 339
dexter Avatar asked Dec 22 '22 21:12

dexter


2 Answers

  1. They bring the ability to take your source code and "understand" it at execution time - LINQ providers being the prime target of this

  2. Well, expression trees are often built from source code (e.g. converted lambda expressions) but instead of that source code being compiled into an executable form it's built into a tree which other code can inspect. (More precisely, the compiler takes source code, and emits executable code which in turn builds an expression tree at execution time.)

  3. Expression trees and functional programming seem somewhat orthogonal to me, to be honest. Both often deal with higher order functions, but that's about it.

  4. MSDN perhaps?

like image 36
Jon Skeet Avatar answered Dec 24 '22 11:12

Jon Skeet


What benefits do expression trees bring to table?

Expression trees allow a program to manipulate a portion of it's own implementation at runtime in a simplified manner. They allow expressions to be passed around as a representation of their structure, rather than as just a delegate that can be invoked.

What exactly does this statement mean: "Expression Trees are data compiled as code"?

Languages prior to C#/.NET have supported this kind of manipulation ... the best example being LISP. The ability to represent the structure of a program in a data structure within the program is referred to as homoiconicity. C# supports a limited for of homoiconicity in the form of expression trees. The C# language allows the creation of expression trees transparently from (a subset) expressions in your code. For example:

int x = 3;
Expression<Func<bool>> IsXLessThan4Expr = () => x < 4;

Func<bool> IsXLessThan4 = () => x < 4;

The variable IsXLessThan4Expr is a captured from the lambda expression as an expression tree. We can now traverse the representation of that expression, if we like to understand what it's structure is - and manipulate it, if we desire. The delegate IsXLessThan4, by contrast, cannot be inspected ... it can only be invoked. You could, of course, always get the raw IL for any method (assuming you have the necessary permissions) - but it's far more difficult to reverse the logical structure of a program from IL than from an expression tree.

What common challenges do the Expression Trees and functional programming in the context of .net solve?

The best example of the use of expression trees to solve a non-trivial problem is in LINQ-to-SQL, where the implementation of IQueryable is able to transform query expression trees written in C# into equvalent SQL queries that can be executed by a database.

Expression trees also make it possible to generate C# code on the fly - since expression trees can be compiled into lambdas. Here's an example of that:

var paramNotification = 
      Expression.Parameter(typeof (NotificationEntry), "noti");

Func<NotificationEntry, bool> predicate = 
        m_PredicateExpr = Expression.Lambda<Func<NotificationEntry, bool>>(
            Expression.LessThan(
                Expression.Property(paramNotification, "Value"),
                Expression.Constant(100)),
            new[] {paramNotification})
        .Compile();

The above snippet creates an expression that compares the Value field of a NotificationEntry object to some supplied constant (100) - and compiles that into a lambda we can call.

What are good online resources to get up the speed on the subject?

MSDN is probably your best bet at the moment.

like image 129
LBushkin Avatar answered Dec 24 '22 10:12

LBushkin