On 07/02/2016 02:45 AM, Patrick Schluter wrote:
On Friday, 1 July 2016 at 20:09:30 UTC, Andrei Alexandrescu wrote:
On 7/1/16 3:43 PM, Patrick Schluter wrote:
On Friday, 1 July 2016 at 16:30:41 UTC, Andrei Alexandrescu wrote:
https://issues.dlang.org/show_bug.cgi?id=16224 -- Andrei

That

    do {
    } while(0)

construct is ridiculous. It's cargo cult at its worst.

That escalated quickly. -- Andrei

Nothing personal. As I said in the initial message, I had to put up with
that construct for the last 15 years because my colleague loves it. So I
had ample time to learn to hate it with a passion.

How would you reshape this? It's important that the call to hook is physically at the end of the function and issued just in that place, and that the code does not do any redundant work.

U hook(U, T)(T value) { return U.init; }

U opCast(U, T)(T payload)
{
        import std.traits;
        do
        {
                static if (!isUnsigned!T && isUnsigned!U &&
                                   T.sizeof <= U.sizeof)
                {
                        if (payload < 0) break; // extra check needed
                }
                auto result = cast(U) payload;
                if (result != payload) break;
                static if (isUnsigned!T && !isUnsigned!U)
                {
                        static assert(U.sizeof <= T.sizeof);
                        if (result < 0) break;
                }
                return result;
        } while (0);
        return hook!U(payload);
}

void main()
{
        opCast!short(42);
}


Thanks,

Andrei

Reply via email to