What to use instead of array.join if RHS is not a range?

Jonathan M Davis jmdavisProg at gmx.com
Tue Nov 27 03:20:35 PST 2012


On Tuesday, November 27, 2012 02:33:00 Andrej Mitrovic wrote:
> This is what I want:
> 
> struct S { int x; }
> 
> void main()
> {
>     S[] arr = [S(2), S(4), S(6)];
>     S s = S(0);
>     arr.join(s);  // fails here
>     assert(arr == [S(2), S(0), S(4), S(0), S(6)]);
> }
> 
> Calling join like that fails, specifically it fails because "s" is not
> a forward range (I don't know why it's implemented like that..). Is
> there some other function which can join with an element as a
> separator?

All you need to do is put it in an array.

arr.join([s]);

should work just fine. Or, if you don't want to allocate on the heap, you could 
do

S[1] s = void;
s[0] = S(0);
arr.join(s[]);

Granted, it _is_ a bit odd that join doesn't accept an element (and I don't 
think that it's the only range-based function with this problem), but it's 
easy enough to turn a single element into a range if you need to.

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list