On 16.07.2010 01:46, BCS wrote:
Hello dsimcha,

 Histogram(someData, 10)
 .barColor(getColor(255, 0, 0))
 .histType(HistType.Probability)
 .toFigure.title("A Histogram")
 .xLabel("Stuff").showAsMain();

With a little meta programming you might be able to make a type that generate
a fluent interface for any type. Using opDispatch you pass the args into
a contained type and return self. The only difference from the users standpoint
is that you need one more function call in the chain.


Great idea. I figured a fancy solution wouldn't be worth it, but if it could be fully generic...


---
import std.stdio;


class Foo {
    int a;
    string b;
    @property int propA(int v) { return a = v; }
    @property int propA() { return a; }
    @property string propB(string v) { return b = v; }
    @property string propB() { return b; }
    void foo() { writeln("foo"); }
}


struct PSet(T)
{
    T _obj;
    typeof(this) opDispatch(string prop, T...)(T val)
    {
        mixin("_obj." ~ prop ~ "=val;");
        return this;
    }
}

PSet!T pset(T)(T obj)
{
    return PSet!T(obj);
}

void main()
{
    Foo f = new Foo;

   // Not sure why putting those foo()'s in there just like that works.
   // compiler bug?
   pset(f).propA(1).foo().propB("something").foo();

    assert(f.a == 1);
    assert(f.b == "something");
}
---

Reply via email to