OT: on IDEs and code writing on steroids
Ary Borenszweig
ary at esperanto.org.ar
Mon May 18 11:53:33 PDT 2009
BCS wrote:
> Reply to Ary,
>
>> BCS wrote:
>>
>>> Reply to Ary,
>>>
>>>> Have you seen Linq? That's *amazing*!
>>>>
>>> LINQ is the only thing c# has the is a notable language feature, but
>>> I don't think it adds anything that puts it much above the rest of
>>> the crowd in any way.
>>>
>>>> You can deal with expression ASTs and do really cool stuff with
>>>> that. Like doing:
>>>>
>>>> var results = someObjectsThatAreGoingToBeTakenFromTheDb.Where(o =>
>>>> o.Name == "Foo");
>>>>
>>> I think this will work:
>>>
>>> int delegate(int delegate(ref T)) Where(T[] array, bool delegate(T)
>>> dg)
>>> {
>>> struct Ret
>>> {
>>> T[] Array;
>>> bool delegate(T) Dg;
>>> int opApply(int delegate(ref T) idg)
>>> {
>>> foreach(ref T t; Array)
>>> if(Dg(t)) if(int ret = idg) return ret;
>>> return ret;
>>> }
>>> return &(Ret(array,dg)).opApply;
>>> }
>>> }
>>> If not, a little tweeking shloudl cover it.
>>>
>> This is for filtering an array. What this does in C# is to translate
>> that code into this (more or less, this is just the idea!):
>>
>> SqlConnection conn = ...;
>> conn.executeQuery("select * from SomeTable Where Name = 'Foo'");
>
> Only for LINQ to SQL (or the like)
>
> all LINQ is is a set of standard nameing conventions and sugar. I Add a
> "Where" function to some SQL tabel object and you get the above as well.
>
>> What the "Where" method does it to receieve an expression tree for "o
>> => o.Name = 'Foo'", and using a visitor it converts it to an SQL
>> statement. In D you don't have expression trees. The best you could do
>> it to give it a string, but then you loose autocompletion,
>> refactoring, nice compiler error messages and probably many other
>> things.
>
> I can see a Where!("Name","Age>")(someName,someAge) being not to hard to
> implement. Heck with prepared statements it might even be trivial, maybe
> even non templated.
>
>>
>> The best thing about this is that the expression is represented using
>> a class, say Func<From, To>. So you could say:
>>
>> Func<From, bool> predicate = (From f) => f.Name == "Foo";
>>
>> Now you can do:
>>
>> From[] array = ...;
>> array.Where(predicate);
>> or:
>>
>> DbObjects.Where(predicate);
>>
>> In the first case, the predicate will be executed at run-time for each
>> object in the array, much like your D example does. In the second
>> case, however, predicate will be translated to SQL.
>>
>
> OK you've got me convinced that c# has a cool feature: not LINQ (it's
> still ho-hum) but AST reflection.
Yes, sorry. Sometimes I mix both things. It's AST reflection that's nice
about C#. Linq is just a nice usage example of it. :-)
More information about the Digitalmars-d
mailing list