On Thursday, 23 March 2023 at 14:36:11 UTC, Alexander Zhirov wrote:
On Thursday, 23 March 2023 at 14:19:31 UTC, user1234 wrote:
omg, let's rewrite this...


Or abuse opBinary

    struct MyVal
    {
        string value;
        T opBinary(string op, T)(T rhs)
        if (op == "+")
        {
            import std.conv : convto = to;
            static if (is(T==int))
                return convto!T(value) + rhs;
            else static if (is(T==float))
                return convto!T(value) + rhs;
            else
                static assert(0);
        }

        alias opBinary this;
    }

    void main()
    {

    auto a = MyVal("100");
    auto b = MyVal("11.2");

int MyInt = a + 0;
float myFloat = b + 0.0f;
}



Reply via email to