On 5/30/18 10:49 AM, DigitalDesigns wrote:
Does it sound good?

class X
{
    double x;
    @property X foo(double y) { x = y; return this; }

    @property X bar(double y) { x = y + 5; return this; }
}

void main()
{
     X x = new X();
     x.foo(3).bar(4);
}


It sort of emulates UFCS but I'm not sure if it's more trouble than it's worth.


I figure it would be better than just returning void as it provides the option to chain but not sure if it will come back to bite me.

Yes, I do this kind of stuff, but you need to word your functions correctly.

I would avoid @property there, as this implies you should use it like:

x.foo = 5;

and if you return a reference to the type itself, it will read weird if you do it that way:

auto five = (x.foo = 5);

Here the name of the function is really really important.

In my use case, I am kind of using it in an SQL builder type, where each time you call a method it adds some piece of the query. Like:

auto query = table.select("id, name").where("foo = 5").orderBy("name");

-Steve

Reply via email to