Thread overview | ||||||
---|---|---|---|---|---|---|
|
March 23, 2014 Syntactic Sugar for Construction of Empty Dynamic Arrays of a Given Type | ||||
---|---|---|---|---|
| ||||
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); } |
March 23, 2014 Re: Syntactic Sugar for Construction of Empty Dynamic Arrays of a Given Type | ||||
---|---|---|---|---|
| ||||
Posted in reply to Nordlöw | 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 |
March 23, 2014 Re: Syntactic Sugar for Construction of Empty Dynamic Arrays of a Given Type | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile | 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
?
|
March 23, 2014 Re: Syntactic Sugar for Construction of Empty Dynamic Arrays of a Given Type | ||||
---|---|---|---|---|
| ||||
Posted in reply to Nordlöw | > 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
|
Copyright © 1999-2021 by the D Language Foundation