phobos by ref or by value

Dan dbdavidson at yahoo.com
Sun Dec 16 07:09:45 PST 2012


Is there a general philosophy in D phobos on when to pass by 
value or
reference?  For instance, to find a slice using lowerBound many 
copies
of the target item, as well as copies of items in the collection 
are
made (see code example below). This seems unnecessary - why not 
have
functions like:

     auto lowerBound(...)(V value)

be:

     auto lowerBound(...)(ref V value)

or:

     auto lowerBound(...)(auto ref V value)

Is this a source for desire for no postblits, shallow semantics on
copy/assignment with additional logic for copy on write 
semantics. If
libraries in general are coded to make many copies of parameters 
it
might be a big improvement to not have postblits. A general 
purpose
library accessing ranges will not know the semantics of V (deep or
shallow), so why incur the cost of copies? Certainly finding a
lowerBound on a range of V can be done with 0 copies of elements?

Is there an established philosophy?

Thanks
Dan
-------
   struct S {
     DateTime date;
     double val;
     this(this) { writeln("copying S ", &this, ' ', date, ',', 
val); }
   }
---------------
   auto before(ref const(ValueType) vt) const {
     auto ass = assumeSorted!orderingPred(_history[]);
     writeln("Before lb");
     auto lb = ass.lowerBound(vt);
     writeln("After lb");
     return History!(V, orderingPred)(_history[0 .. lb.length]);
   }

---------------
Before lb
copying S 7FFF622CE110 2001-Nov-01 00:00:00,0
copying S 7FFF622CE090 2001-Nov-01 00:00:00,0
copying S 7FFF622CE020 2001-Jan-01 00:00:00,100
copying S 7FFF622CE030 2001-Nov-01 00:00:00,0
copying S 7FFF622CDF90 2001-Jan-01 00:00:00,100
copying S 7FFF622CDFA0 2001-Nov-01 00:00:00,0
copying S 7FFF622CE020 2002-Jan-01 00:00:00,200
copying S 7FFF622CE030 2001-Nov-01 00:00:00,0
copying S 7FFF622CDF90 2002-Jan-01 00:00:00,200
copying S 7FFF622CDFA0 2001-Nov-01 00:00:00,0
copying S 7FFF622CE020 2001-Jan-01 00:00:00,200
copying S 7FFF622CE030 2001-Nov-01 00:00:00,0
copying S 7FFF622CDF90 2001-Jan-01 00:00:00,200
copying S 7FFF622CDFA0 2001-Nov-01 00:00:00,0
After lb


More information about the Digitalmars-d-learn mailing list