www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Syntactic Sugar for Construction of Empty Dynamic Arrays of a Given

reply =?UTF-8?B?Ik5vcmRsw7Z3Ig==?= <per.nordlow gmail.com> writes:
Is there a convenicene function for assigning an empty dynamic 
array of a given type to a variable?

     cast(int[])[];

I'm using this in ForwardDifference constructor:

auto forwardDifference(R)(R r) if (isInputRange!R)
{
     import std.range: front, empty, popFront, dropOne;

     struct ForwardDifference
     {
         R _range;
         alias E = ElementType!R;                       // Input 
ElementType
         alias D = typeof(_range.front - _range.front); // Element 
Difference Type. TODO: Use this as ElementType of range
         D _front;
         bool _initialized = false;

         this(R range)
         in { assert(!range.empty); }
         body {
             auto tmp = range;
             if (tmp.dropOne.empty) // TODO: This may be an 
unneccesary cost but is practical to remove extra logic
                 static if (isArray!R) // TODO: Construct R in a 
generic way that include dynamic arrays?
                     _range = cast(D[])[];
                 else
                     _range = R(); // return empty range
             else
                 _range = range; // store range internally (by 
reference)
         }

          property:
         auto ref front() {
             if (!_initialized) { popFront(); }
             return _front;
         }
         auto ref moveFront() {
             popFront();
             return _front;
         }
         void popFront() {
             if (empty is false) {
                 _initialized = true;
                 E rf = _range.front;
                 _range.popFront();
                 if (_range.empty is false)
                 {
                     _front = _range.front - rf;
                 }
             }
         }
         bool empty() {
             return _range.empty;
         }
     }

     return ForwardDifference(r);
}
Mar 23 2014
parent reply "bearophile" <bearophileHUGS lycos.com> writes:
Nordlöw:

 Is there a convenicene function for assigning an empty dynamic 
 array of a given type to a variable?

     cast(int[])[];
                     _range = cast(D[])[];
If the variable is already typed, you can use [] otherwise you can use cast(T)[] or (T[]).init to avoid casts. Bye, bearophile
Mar 23 2014
parent reply =?UTF-8?B?Ik5vcmRsw7Z3Ig==?= <per.nordlow gmail.com> writes:
On Sunday, 23 March 2014 at 14:58:53 UTC, bearophile wrote:
 Nordlöw:

 Is there a convenicene function for assigning an empty dynamic 
 array of a given type to a variable?

    cast(int[])[];
                    _range = cast(D[])[];
If the variable is already typed, you can use [] otherwise you can use cast(T)[] or (T[]).init to avoid casts. Bye, bearophile
Ok. Enough for now...but Is there a way to avoid this array special handling: static if (isArray!R) _range = (D[]).init; else _range = R(); // return empty range through some initializer expression for _range that covers both R() and (D[]).init ?
Mar 23 2014
parent =?UTF-8?B?Ik5vcmRsw7Z3Ig==?= <per.nordlow gmail.com> writes:
 Ok. Enough for now...but Is there a way to avoid this array 
 special handling:

                 static if (isArray!R)
                     _range = (D[]).init;
                 else
                     _range = R(); // return empty range

 through some initializer expression for _range that covers both

     R()

 and

     (D[]).init

 ?
The answer was right in front of me... _range = R.init
Mar 23 2014