how to build up the library..

Jacob Carlborg doob at me.com
Fri Oct 7 04:25:02 PDT 2011


On 2011-10-07 09:26, Russel Winder wrote:
> On Fri, 2011-10-07 at 08:49 +0200, Jacob Carlborg wrote:
> [ . . . ]
>> I think it's important to support the range interface (or if it's
>> simpler, the array interface). I think ActiveRecord has a good high
>> level API which allows to lazily evaluate SQL queries.
>>
>> persons = Person.all # no query is performed here
>> persons = persons.where("name = 'Joe'") # or here
>>
>> persons.each do |p| # The actual query is performed here
>>       # do something with each Person
>> end
>>
>> I don't think it would be strange to see something like this:
>>
>> Person.where("age>  10").map{ |p| p.name } # using "map" on an SQL query
>
> Not just Ruby an Rails with ActiveRecord.  Groovy/Grails/GORM does what
> is basically the same.  Python cannot do the same in the same way Ruby
> and Groovy can, but it has it equivalent (augmented with SQLAlchemy).
> Basically it comes down to constructing dynamic queries using the
> dynamic and MOP nature of the languages.  The crucial keyword here is
> Builder.  The trick is that what looks like function call is actually
> interpreted via the builder/MOP as the construction of a data structure,
> which then creates the query on demand -- with template placeholder
> filling as required.
>
> Does D have the reflection capabilities to do this in any way, shape or
> form similar tp the way the dynamic languages do it?

It depends on what you want to achieve. If you want just the above 
example (and similar) to work, including lazy loading that would be 
possible and not very hard.

If you want a more complete ORM and do things like (in Ruby):

person = Person.find_all_by_name_and_age("Joe", 15)
p person.name

That's a little harder. You could use opDispatch to make 
"find_all_by_name_and_age" or "name" work, but not both. The problem is 
that you can not overload methods on static and you are not forced to 
call a static method prefixed with the class name.

class Foo
{
     void bar () {};
     static void bar () {};
}

Currently the above doesn't work. You can also call a static method on 
an instance, which will also cause problems. What we want here is to be 
able to have two versions of opDispatch, one static and one non-static.

-- 
/Jacob Carlborg


More information about the Digitalmars-d mailing list