LINQ (Language Integrated Query) is a new feature of the forthcoming .NET framework revision (post-Vista!) and C# 3.0 that’ll let you use an SQL-like query language imposed by the language, framework and compiler to do, basically, queries on any data structure. (Channel 9 has a video on LINQ and it is definitely worthwhile. DLinq, the database abstraction layer, also happens to implement ActiveRecord. Very impressive.)
What do we need to add to Obj-C or to Cocoa to make this happen? LINQ works by importing the namespace System.Query, which adds methods to every object implementing the IEnumerable<T> interface (enumerable and returns a specific type). We can do that.
We can’t directly add categories to everything implementing an Obj-C protocol (there’s no NSEnumeratorProviding), but we can add a category on NSObject, and do some inquisitive poking and prodding in the vein of [self respondsToSelector:@selector(linqObjectEnumerator)], and add categories for the Foundation collections that fall through to the correct enumerator methods (most commonly - objectEnumerator). Based on calling this and seeing if it returns anything, we could selectively implement methods for the clauses.
We also have a way of specifying predicates (NSPredicate) and drilling down data structures (key-value paths). However, this is about how far you can go. What’s left? For one thing, the integration of the query into the language. Another is a more human and flexible way of specifying NSPredicates - like any other comparison in Obj-C, basically. If you watch that video again, you’ll see the huge disconnect between filtering an array and offering what LINQ offers.
What’s interesting about LINQ is that half of it is based on C# (or ultimately MSIL) catching-up to Obj-C in things like adding methods to existing classes or inventing stuff only needed by being strongly-early-typed (a result is actually an anonymous type with properties having inferred names, each predicate comparison is a lambda expression, and the whole deal drops tens of new keywords into C#). The other half is the .NET side of things driving off in the distance, leaving the Cocoa lover in me drooling at my keyboard. Cocoa, and its data access in specific, could grow enormously by stealing the right things from LINQ and I’m hoping that this is on Apple’s radar (hint, hint) if not in Leopard then at least in time for 10.6.