I am trying to learn D and (partly) replace my C++ projects with D. Lately in C++ I have become a fan of the type of functional programming discussed here:
http://blog.knatten.org/2012/11/02/efficient-pure-functional-programming-in-c-using-move-semantics/
and was wondering if something similar is possible in D.

Basically the idea is to define functions as follows:

std::vector<double> add_to_vector( double x, std::vector<double> &&v ) {
 v.push_back(x);
 return v;
}

and call it as follows:

std::vector<double> v;
v = add_to_vector( 1.2, std::move( v ) );

I know I could do the same by passing a reference value, but this makes it explicit that I am changing v, while being as efficient as passing a reference (since it just reuses the memory allocated to v instead of making a copy).

Is this possible in D? I had a look through many tutorials and some of the docs, but could not find out how to do it.

Any suggestions would be appreciated.

Reply via email to