Skip to content
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

Add a trivial apply function #59

Open
smitsyn opened this issue Sep 4, 2023 · 0 comments
Open

Add a trivial apply function #59

smitsyn opened this issue Sep 4, 2023 · 0 comments

Comments

@smitsyn
Copy link

smitsyn commented Sep 4, 2023

Imagine a scenario where a several transformations have to be applied to an optional:

optional<Tb> func_a(Ta);
optional<Tc> func_b(optional<Tb>);
optional<Td> func_c(Tc);

optional<Ta> opt;

Currently this is done in this way, where orders in which functions are written and applied is inconsistent:

func_b(opt.and_then(func_a)).and_then(func_c);

I propose adding a trivial member function apply(F f) to optional that applies f to *this, so the code above can be written as:

opt.and_then(func_a).apply(func_b).and_then(func_c);

In this example, function names are written down in the order they are called, so it looks much cleaner. Probably if we could have UFCS, this wouldn't be needed.

In real world these functions could be lambdas written in-line and consisting of several lines, so this order inconsistency may look really ugly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant