Is it possible to handle 'magic' property assignments a'la PHP?

Philippe Sigaud philippe.sigaud at gmail.com
Mon Jan 6 13:07:36 PST 2014


>> I didn't know that, thanks. I read it during the holidays in Martin
>> Fowler's book on DSL, but indeed that book is from 2005, IIRC.
>
>
> That's a bit old :). According to this site[1] Rails 1.0 was released in
> December 2005. Rails 4.0 was released in June 2013.

Ouch, that was 2010, my bad.


>
> I think there are two reasons why they deprecated that syntax:
>
> * They want to avoid method_missing (same as opDispatch in D)
>
> * They want to have the name of a column close to the value of that column.
> Example:
>
> Table.find_first_by_fist_name_and_last_name("foo", "bar")
>
> In the above example the values and the columns they belongs to are quite
> disconnected. The column names are far to the right and the values are far
> to the left. With the AA syntax you get the column name and its value
> closely connected. It's clear to see which value belongs to which column.

Yes indeed. Too clever for its own good.


> I have to admit that it looks a lot better in Ruby than in D.

Ruby does have a clean syntax (though I find blocks to be a bit heavy).

>
>
>> Some other possibilities could be:
>>
>> Table.where!(e => e.first_name == "foo").first; // Similar to
>> std.range.filter
>
>
> This is the one I like best and it's possible to have the same syntax in
> Ruby as well with a plugin, Squeel[2]. This allows to have more advanced
> quires containing "or" and negation.
>
> The problem is it's basically only equality that works with this syntax. You
> cannot overload !=, && or || in D. You could of course use method names like
> "equals", "and" and "or".

But I'm not overloading any syntax here. I'm using a closure, not an
expression template.

Table.where!(e => e.first_name != "foo" && e.first_name.length > 4).first;


More information about the Digitalmars-d-learn mailing list