InfoQ

News

Edit Expression Trees with MetaLinq

Posted by Jonathan Allen on Jun 14, 2007 12:53 AM

Community
.NET
Topics
Dynamic Languages
Tags
LINQ,
Metaprogramming

Expression trees are a powerful feature in any language. LINQ uses them under the covers to execute object queries, while DLINQ to SQL transforms them into SQL queries. Other people are using them to generate code on the fly. But like any other feature, there are design tradeoffs.

In order to better support things like query comprehension and function composition, expression trees are immutable. Libraries are guaranteed that once they have a reference to an expression tree, that expression tree will never change. Whole classes of bugs are eliminated by doing this, though it does make modified versions of an expression tree somewhat difficult.

Developers wishing to edit one have to manually build new expressions by copying existing ones. Aaron Erickson's MetaLinq allows developers to side-step this by providing a way to convert expression trees into mutable representations and back again.

MetaLinq does this with what Aaron calls an Editable Shadow. Developers create this shadow by passing an expression to the CreateEditableExpression method. When done making changes, they simply need to call ToExpression to get a usable expression tree again.

MetaLinq isn't the only way to edit expression trees. Jomo Fisher demonstrates a visitor pattern than will walk an expression tree, creating a new tree while swapping out specific nodes. While this technique is more complex conceptually, it does allow for some interesting search and replace techniques.

No comments

Reply

Exclusive Content

Agile in Practice: What Is Actually Going On Out There?

Scott Ambler talks about actual data resulting from surveys made during 2006-2008, showing how Agile is perceived and implemented within organizations.

Building Smart Windows Applications

From QCon 2008, Daniel Moth presents on using Visual Studio 2008 and .NET 3.5 to create compelling rich Windows applications.

Joshua Kerievsky about Industrial XP

Joshua Kerievsky, founder of Industrial Logic, talks about Industrial Extreme Programming which extends XP by including practices dealing with management, customers and developers.

Jeff Barr Discusses Amazon Web Services

Amazon Web Services (AWS) Evangelist Jeff Barr discusses SimpleDB, S3, EC2, SQS, cloud computing, how different Amazon services interact, origins of AWS, AWS globalization and the March AWS outage.

More Than Just Spin (Up) : Virtualization for the Enterprise and SaaS

Cloud services have helped bring virtualization to the forefront. Its full power however, also includes other benefits such as high availability, disaster recovery, and rapid provisioning.

Ruby Beyond Rails

John Lam talks about his path to dynamic languages, some of the problems of making IronRuby run fast, and how the DLR helps with implementing languages.

VMware Infrastructure 3 Book Excerpt and Author Interview

VMware Infrastructure 3: Advanced Technical Design Guide and Advanced Operations Guide provides a wealth of practical insights into setting up virtualization in todays corporate environments.

Architectures of extraordinarily large, self-sustaining systems

Can a system that is so large it cannot be comprehended be "designed" in a conventional sense? The foundations of computing are about to change. In this talk, Richard P. Gabriel explores why and how.