Skip to content

UFCS: add method chaining #17

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed

Conversation

filipsajdak
Copy link
Contributor

@filipsajdak filipsajdak commented Sep 26, 2022

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)

@hsutter
Copy link
Owner

hsutter commented Oct 1, 2022

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.

@filipsajdak
Copy link
Contributor Author

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 filipsajdak force-pushed the fsajdak-ufcs-add-method-chaining branch 2 times, most recently from 6d78544 to 055b61e Compare October 6, 2022 08:40
@filipsajdak filipsajdak force-pushed the fsajdak-ufcs-add-method-chaining branch 2 times, most recently from b067266 to 8dac3b2 Compare October 10, 2022 22:19
@filipsajdak filipsajdak force-pushed the fsajdak-ufcs-add-method-chaining branch from 8dac3b2 to 5e463f0 Compare October 18, 2022 17:19
@filipsajdak filipsajdak force-pushed the fsajdak-ufcs-add-method-chaining branch from 5e463f0 to 783312e Compare October 30, 2022 21:39
@filipsajdak filipsajdak force-pushed the fsajdak-ufcs-add-method-chaining branch 2 times, most recently from 7566599 to 14415a7 Compare December 6, 2022 22:17
Former implementation was not supporting method chaining

```cpp
v.a().b().c(); // failed to use UFCS
```

The change converts above code into nested call of `CPP2_UFCS` macros:

```cpp
CPP2_UFCS_0(c, CPP2_UFCS_0(b, CPP2_UFCS_0(a, v) ) );
```

Functions with expr-list are also handled.
@filipsajdak filipsajdak force-pushed the fsajdak-ufcs-add-method-chaining branch from 14415a7 to b035008 Compare December 15, 2022 23:03
@hsutter
Copy link
Owner

hsutter commented Dec 16, 2022

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

@filipsajdak
Copy link
Contributor Author

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
Copy link
Contributor Author

@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 #18

@filipsajdak
Copy link
Contributor Author

@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
Copy link
Contributor Author

This PR is replaced by: #169

@filipsajdak filipsajdak deleted the fsajdak-ufcs-add-method-chaining branch December 22, 2022 10:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants