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

Philippe Sigaud philippe.sigaud at gmail.com
Sun Jan 5 13:44:34 PST 2014


On Sun, Jan 5, 2014 at 5:18 PM, Jacob Carlborg <doob at me.com> wrote:

> Just for the record. In Rails, that's the old, now discourage, Rails 2
> syntax.

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.

> In Rails 3 and later the following syntax is preferred:
>
> Table.where(first_name: "foo").first
>
> Which in D would look like:
>
> Table.where(["first_name": "foo"]).first;

Yes, using AA is a nice idea, which avoids introducing first_name in
the current scope.

Some other possibilities could be:

Table.where!(e => e.first_name == "foo").first; // Similar to std.range.filter

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

Table.where((string first_name) => first_name == "foo").first; // By
extracting the parameter name

The syntax is heavier than your example, but some nice logic can put
into a closure.

Some also advocate:

Table.where.first_name.equals("foo").first;

But I find it a bit too clunky.


More information about the Digitalmars-d-learn mailing list