Who wore it better?

Marco Leise via Digitalmars-d digitalmars-d at puremagic.com
Fri Apr 15 13:47:17 PDT 2016


Am Fri, 15 Apr 2016 14:48:26 -0400
schrieb Andrei Alexandrescu <SeeWebsiteForEmail at erdani.org>:

> 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.

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.

Just my 2ยข.

-- 
Marco



More information about the Digitalmars-d mailing list