Archive for the ‘LINQ’ Category.

Expression Trees: abusing operator overloading for fun and profit

Quick, what should this routine produce?

procedure Test;
   expr: TExpression;
   result: string;
   expr := 'Value';
   expr := expr = 5;
   result := expr;

There are three basic answers here:

  1. Wait, you’re assigning a string to it, and then a boolean comparison against an integer… does that even compile?
  2. Well, first you’re assigning a string, then a boolean comparison against an integer, then turning it into a string… well, TExpression must be some sort of thing like a Variant.  So the output should be “False”.
  3. If TExpression is a record, the output could be just about anything.

If you answered 3), you’re probably Stefan Glienke, or someone like him who already knows the trick.  When I do this, the output is “(Value = 5)”.  If you want to know how that’s possible in ordinary Delphi code, read on. Continue reading ‘Expression Trees: abusing operator overloading for fun and profit’ »

More dwsLinq improvements: query sources and JSON

When I started writing dwsLinq, I never intended it to just be an integrated SQL query builder.  I did that first because it was both useful and easy to implement, but that was never the end goal.

Over the last few days I’ve taken a big step towards the real goal: developing a general-purpose data querying system.  I factored out all of the SQL-specific stuff into its own unit and created an interface called IQueryBuilder that responds to the various query operators to build an expression tree that can be evaluated by DWS.  You create a recognizer and register it with dwsLinq, that checks the value of your FROM expression and returns an IQueryBuilder if it can create an expression for it.  And once I had that working, I created a second unit with a new implementation of IQueryBuilder, for querying JSON data. Continue reading ‘More dwsLinq improvements: query sources and JSON’ »

dwsLinq update: INTO

I just checked in an update to dwsLinq that covers a big step in its progress: the INTO clause.

This is not quite the same thing as INTO in C#’s version of LINQ.  INTO there basically acts as a naming operator, taking a query expression and giving it a name so that you can use it as a sub-expression in further query work.

I did something different with this.  INTO in dwsLinq takes a function pointer as its argument.  The function has to have a single parameter of type Dataset, and return some value, and the INTO clause changes the output of the entire query expression. Continue reading ‘dwsLinq update: INTO’ »

Work in progress: dwsLinq extension for DWS

I’ve always enjoyed the concept behind LINQ: write SQL-esque queries directly in your code, with the compiler able to provide syntactical and type checks, and automatically generate the correct code to do what you wanted.

Unfortunately, there’s nothing like that available for Delphi.  There could be, if the team would shift their focus to augmenting the language with actual useful features borrowed from managed languages, such as a proper extension method implementation, rather that burdening it with useless, counterproductive crap like pseudo-garbage collection and new String models.  But for the time being, we’re out of luck.

It could be done in DWS, but LINQ is a really complex system that makes heavy use of lambdas (aka anonymous methods,) which Eric Grange hasn’t gotten around to supporting yet outside of the JavaScript cross-compilation.  Apparently it’ll take a complete re-architecting of the execution model to make anonymous methods available.

But a while ago, an idea struck me.  In .NET, LINQ uses anonymous methods to support expression class evaluation at runtime.  But DWS is already an interpreted scripting system that operates on an expression tree!  And since DWS has a language extension mechanism in place, I decided to poke around and see what was possible. Continue reading ‘Work in progress: dwsLinq extension for DWS’ »

Native LINQ under active development?

I got a very interesting response to my last post, about missing features in extended RTTI.  Barry Kelly wrote in a comment,

There wasn’t any increase in [RTTI] coverage for XE because other work had priority (64-bit, x-plat, a front end that could support LINQ, more things that I can’t talk about).

None of this actually showed up in XE.  The cross-platform work was supposed to, right up until a couple months ago, but they deferred it because it wasn’t ready yet.  And the 64-bit work’s been promised but not delivered yet for a long time.  (Now they say it’ll be in XE2.  I sure hope so!)  But… LINQ support in the compiler?  Under active development and not a “more things I can’t talk about”?  Wow, when did this happen?
Continue reading ‘Native LINQ under active development?’ »