overload binary + operator to work with different types

Steven Schveighoffer schveiguy at yahoo.com
Tue Mar 13 19:39:56 UTC 2018


On 3/13/18 3:28 PM, Simen Kjærås wrote:

> Now, since we've established that there's no way to do exactly what you 
> want, maybe it's time to take a look at what you actually want. :p Why 
> do you want to write that code? Why would a.append(1, 2, 3); not be good 
> enough?

Typically, we can do this with arrays quite easily, especially strings:

arr ~= [1, 2, 3];
str ~= "123";

It would be nice to have another way to do this. a.append(1, 2, 3) would 
work , and you can do it in a nice variadic way with:

append(T[] vals...)

And this won't allocate anything on the heap (this is essentially how I 
did it in dcollections, see 
https://github.com/schveiguy/dcollections/blob/master/dcollections/model/Addable.d#L55).

But most D containers, you would expect to be able to append a range. 
This is what I would recommend. It will work for arrays as well.

-Steve


More information about the Digitalmars-d-learn mailing list