LICPP icon indicating copy to clipboard operation
LICPP copied to clipboard

Need an elegant way to implement `multiple-value-bind`

Open Z-Shang opened this issue 6 years ago • 2 comments

It is currently impossible to implement multiple-value-bind.

In fact we can do something similar to std::tie by:

type foo, bar;
mvb(a_cons, cons(&foo, &bar), rest);

But this requires variable declaration in advance, which is not what we want.

A well-packed dirty hack is acceptable as long as it is not directly exposed to the user.

Z-Shang avatar Mar 12 '18 18:03 Z-Shang

The current implementation is:

a helper function _mvb that binds a list of values to a list of pointers (basically, by assignment)

a type _values_t that evals into the type of the list of pointers corresponding to the list of values

finally the wrapper function multiple_value_bind that takes a list as its first argument, and rest pointers of variables, it will bind the values and return a lambda expression that takes an expression, after the expression, bind the original values of the pointers back to them.

Z-Shang avatar Mar 13 '18 17:03 Z-Shang

Maybe std::bind is a good direction to give a try

Z-Shang avatar Mar 19 '18 16:03 Z-Shang