On 04/15/2016 04:47 PM, Marco Leise wrote:
Am Fri, 15 Apr 2016 14:48:26 -0400
schrieb Andrei Alexandrescu <[email protected]>:

On 4/15/16 2:46 PM, Steven Schveighoffer wrote:
On 4/15/16 1:24 PM, Andrei Alexandrescu wrote:
auto overlap(T, U)(T[] r1, U[] r2) @trusted pure nothrow
if (is(typeof(r1.ptr < r2.ptr) == bool))
{
      import std.algorithm : min, max;
      auto b = max(r1.ptr, r2.ptr);
      auto e = min(r1.ptr + r1.length, r2.ptr + r2.length);
      return b < e ? b[0 .. e - b] : null;
}

Who wore it better?

inout(T)[] overlap(T)(inout(T)[] r1, inout(T)[] r2) @trusted pure nothrow
{
      import std.algorithm: min, max;
      auto b = max(r1.ptr, r2.ptr);
      auto e = min(r1.ptr + r1.length, r2.ptr + r2.length);
      return b < e ? b[0 .. e - b] : null;
}

Is that better or worse than the one without inout? -- Andrei

I tend to write functions in the latter style, to avoid
unneeded template instantiations. In this case it also
documents the intent better. Both arrays are of the same type
T and we wont modify the contents.

Thanks, this is a good point that needs keeping in mind.

Even if the compiler can remove binary identical instances
after the fact, the compile time and memory use increases.
It also has implications on debugging. After duplicate
removal, you cannot map a function address to a unique symbol
name any more.

This is perhaps weaker.


Andrei

Reply via email to