cppfront
cppfront copied to clipboard
UFCS: add method chaining
I have been playing with UFCS. I found that it doesn't support method chaining yet. So I have added it.
Former implementation was not supporting the method chaining - the below code failed to be compiled
v.a().b(1,"arg").c(); // failed to use UFCS
The proposed change converts the above code into a nested call of CPP2_UFCS
macros:
CPP2_UFCS_0(c, CPP2_UFCS_0(b, CPP2_UFCS_0(a, v), 1, "arg" ) );
This allows using ranges library without the need of operator|
for chaining. operator|
does not work in cppfront at the moment. What I realized is that having UFCS that supports chaining solves the problem.
I don't have ranges working on my current system, so I put a custom example in the regression test directory.
Having the possibility of method chaining allows us to use smart pointers and pass them to the functions that need a raw pointers.
smart.get().func(); // UFCS will be executed on raw pointer (thanks to chaining)
Interim ack: Thanks! Yes, I've been meaning to add chaining. I was thinking of implementing it later in the same function and remove this special case part of the function (I dislike that there are two paths that deal with .
operator differently)... let me keep this PR open without action until I get a chance to look at it again later.
One thing I found is that it is not working when called on a constructor call - maybe I will try to add it.
std::string("this will not work").method();
and this
s := std::string("this will work");
s.method();
will work.
Picking this up: How does this relate to #18?
It is for the cases when the first call is done on the object by calling its method.
#18 is for a case when there is a free function call and the chaining starts on the function result.
@hsutter sorry that I did not deliver it yet. I am trying to catch up with other changes and resolve all issues found in regression tests. I am working on it.
I will try to merge this with https://github.com/hsutter/cppfront/pull/18
@hsutter I managed to rewrite my code to match the latest changes. It allows me to simplify, make it more generic, and merge it with #18. I will clean it up and push the change... It's already 3:30 am... I need to go to sleep and will finalize it in a couple of hours.
This PR is replaced by: https://github.com/hsutter/cppfront/pull/169