cppfront icon indicating copy to clipboard operation
cppfront copied to clipboard

UFCS: add method chaining

Open filipsajdak opened this issue 2 years ago • 2 comments

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)

filipsajdak avatar Sep 26 '22 00:09 filipsajdak

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.

hsutter avatar Oct 01 '22 23:10 hsutter

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.

filipsajdak avatar Oct 02 '22 00:10 filipsajdak

Picking this up: How does this relate to #18?

hsutter avatar Dec 16 '22 23:12 hsutter

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.

filipsajdak avatar Dec 17 '22 00:12 filipsajdak

@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

filipsajdak avatar Dec 18 '22 18:12 filipsajdak

@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.

filipsajdak avatar Dec 19 '22 02:12 filipsajdak

This PR is replaced by: https://github.com/hsutter/cppfront/pull/169

filipsajdak avatar Dec 20 '22 01:12 filipsajdak