tomhrr / dale

Lisp-flavoured C
BSD 3-Clause "New" or "Revised" License
1.03k stars 48 forks source link

defining extra function for relatively changing a variable neccessary for good performance? #135

Closed porky11 closed 7 years ago

porky11 commented 7 years ago

In other system programming languages, you should define specialized operations for arithmetic operations (like operator+= in c++ and AddAssign in Rust) retval seems to make this unneccessary, but this is not clear in the documentation. Would (setv a (+ a b)) when called some array-like types, write the results of each sum directly to a, if this + is a retval-func? or is this only true for newly initialized variables? it would be a benefit over c++, if this is the case for setf, and should be documented more clearly.

tomhrr commented 7 years ago

On Fri, Sep 30, 2016 at 04:59:41PM -0700, Fabio Krapohl wrote:

In other system programming languages, you should define specialized operations for arithmetic operations (like operator+= in c++ and AddAssign in Rust) retval seems to make this unneccessary, but this is not clear in the documentation. Would (setv a (+ a b)) when called some array-like types, write the results of each sum directly to a, if this + is a retval-func? or is this only true for newly initialized variables? it would be a benefit over c++, if this is the case, and should be documented more clearly.

It doesn't cover the operator+= case: the retval binding provided to a retval function is always a pointer to a freshly-initialised value of the relevant type. It's essentially a more explicit version of C++'s named return value optimisation.

porky11 commented 7 years ago

Would be nice, if retval would point to the set variable. A macro, which acts similar to retval-functions, but in this way, shouldn't be too difficult to write.

tomhrr commented 7 years ago

I think it would be unintuitive if retvals were permitted to access the state of the variable for the purposes of mutating it. While custom setf functions can access the state of the target variable, there is a legitimate expectation on the part of an author that these functions overwrite the target state with the argument state, putting aside any internal bookkeeping or similar that they might do.

porky11 commented 7 years ago

I already had examples where overriding retvals may cause problems (matrix multiplication)