On Wednesday, 13 June 2018 at 17:37:44 UTC, Steven Schveighoffer wrote:

Hm... the only way to do it in D is to provide a function that checks whether the small vector optimization is in play, and return a pointer/slice to itself.

With D it is possible to alias the getter function that provides the actual data to allow code to look nicer.

For example (crude example):

struct Vector(T)
{
   bool svo; // small vector optimization
   union
   {
      T[4] local;
      T[] heap;
   }
   inout(T)[] get() inout { return svo ? local[], heap; }
   alias get this;
   ... // implement specialized append, concat operators, etc.
}

Now, you can use Vector as if it were an array, and it just works.

-Steve

Thanks for an idea, I will try it.

Reply via email to