On Thursday, 25 June 2015 at 01:32:22 UTC, Timon Gehr wrote:
[...]

Heres what I came up with... I love D so much <3

module util.binOpProxy;

import std.algorithm : joiner, map;
import std.array : array;
struct __typeproxy(T, string s)
{
        enum op = s;
        T payload;
        auto opUnary(string newop)()
        {
                return __typeproxy!(T,newop~op)(payload);
        }
}

/**
 * Example:
 * struct test
 * {
 *     mixin(binOpProxy!("~", "*"));
 *
 *     void opBinary(string op : "+~~", T)(T rhs)
 *     {
 *         writeln("hello!");
 *     }
 *
 *     void opBinary(string op : "+~+-~*--+++----*", T)(T rhs)
 *     {
 *         writeln("world");
 *     }
 *
 *     void opBinary(string op, T)(T rhs)
 *     {
 *         writeln("default");
 *     }
 * }
 *
 */
enum binOpProxy(proxies ...) = `
    import ` ~ __MODULE__ ~ ` : __typeproxy;
auto opBinary(string op, D : __typeproxy!(T, T_op), T, string T_op) (D rhs)
    {
        return opBinary!(op~D.op)(rhs.payload);
    }
` ~ [proxies].map!((string a) => `
    auto opUnary(string op : "` ~ a ~ `")()
    {
        return __typeproxy!(typeof(this),op)(this);
    }
`).joiner.array;


Reply via email to