Wednesday, September 21, 2005

C-Pound is Looking Sharp

I took the time to view the video interview about the LINQ project and C# 3.0's first-class query mechanisms. I was actually impressed with it, and it solved some problems I have with Python. It also made me think how the same solutions could be applied in Python, or any such dynamic language.


What is most interesting about LINQ, I couldn't be sure of from the video. Does it pull in all the data and apply the anonymous functions to it for conditional filtering, or does it take the where clause and actually generate that into a request to the server? Now, this doesn't matter for XML documents and especially direct queries on CRL objects, but with relational tables it really is important.


This is what Python needs: a generalized way to generate patterns to match, using existing operators. There are some tricks available, like allowing sometype==value to return a callable that can be passed an instance of sometype and return True if it equals value. This can be used to generate lots of pattern matchers. The problem is that some things aren't so easily redefined, like and and or operators, and it isn't easy to define that operation if you also want == for instances of sometype. That requires defining an instance method and class method of the same name for one type. Possible, but very tricky.


It would be very useful to add first-class pattern matching to Python. I'm thinking of a syntax along the lines of ?==x to generate a pattern object representing the pattern needed. What is important, however, is that it would create more than an object that can be called to test if something matches the pattern, but other code would be able to introspect the pattern, merge patterns together, generate SQL code from them, etc.


I wonder how well the community would respond to this. It would probably be best to promote a common and hopefully standard pattern package, and to propose syntax integration of the module at a later date. That would also give time for the pattern matchings to evolve. Hopefully this will happen, because Python is improving every day, but if it doesn't do so in the right places, it will fall from its rising seat of glory.


(More examples of the Pattern Syntax)
(All comparison operators work as expected, such as `?>10' to test for values greater than 10)

`?foo' to test for an instance of foo

`?is foo' to test is something is foo

`?in foo' to test is something is in foo

`?contains foo' to test if foo is something

`?hasattr foo' to test if something has the attribute 'foo'

`?attr foo ?==10' to test if something's 'foo' attribute is equal to 10

`bar?foo' to test if bar is of type foo, where bar is the name of an iterated object, such as in a for loop or list comprehension. This can be used to clarify when there are more than one object per loop to test.

`?[4]?=="four"' to test if the 4th item in a list-like object is equal to the string "four".

`?foo,bar' test if something is of type foo or type bar

`?all foo,bar' test if something is of both type foo and type bar


Any thoughts on this?

No comments: