Comments (16)
Hi @hugoetchegoyen, thanks for bringing this up and that is a very good topic.
"expr" was introduced one year ago and now we have a chance to rethink about it.
Maybe there is a chance to remove all its usage and provide overloaded versions for
nullary callable, Id, and others for "when" and "operator=".
Then the sample would be like
#include "matchit.h"
template<typename T1, typename T2>
constexpr auto eval(std::tuple<char, T1, T2> const& exp)
{
using namespace matchit;
Id<T1> i;
Id<T2> j;
return match(exp)
(
pattern | ds('+', i, j) = i + j,
pattern | ds('-', i, j) = i - j,
pattern | ds('*', i, j) | when(i) = i, // previously not allowed
pattern | ds('/', i, j) = 12345, // previously not allowed
pattern | _ = []
{
assert(false);
return -1;
}
);
}
The only concern is about others
. Would that be too risky if users provide a unary instead of nullary mistakenly and we silent the error and return the unary function as the result?
A compromise may be to keep expr for others
only and deprecate its usage for Id
.
What do you think?
Thanks,
Bowen
from matchit.cpp.
from matchit.cpp.
PS:
With my three trivial overloads the situation is like your second alternative: expr() is compulsory for others only. But it is not deprecated, on the contrary, it is generalized to accept everything.
from matchit.cpp.
@hugoetchegoyen good pointer about "this doesn't deserve a special protection". And by deprecate I mean marked it with "[[deprecated]]", so to discourage its usage and remove in a later major version release.
If you are interested, will you create a pull request for it?
Thanks,
Bowen
from matchit.cpp.
Bowen,
I already made a quick try to overload when() and operator= for others too, but failed, I will make a more serious attempt as soon as I find some free time and I will let you know. Then if you are satisfied with the result I can make a pull request, but first I have to find out how since my git skills are very limited.
Regards,
Hugo
from matchit.cpp.
Hi Bowen,
I think I suceeded. The changes are very small. You can use 'others' as when predicates or handlers now. I attach a file with the changes and the sample program. Function expr() is still available, I used it (unnecessarily) in expr(i+j). If you would like to remove it, I guess it can be moved to namespace impl.
Regards,
Hugo
from matchit.cpp.
@hugoetchegoyen looks good to me. And you can also define a helper toNullary function inside impl namespace and than you will only need one version of when / operator=.
Are you planning to create a pull request yourself or you want me to create one for you?
Please let me know if you meet any issues when creating a PR.
To create a PR, you need to fork the repo and commit the changes to your fork repo firstly and then create a PR from your fork repo to this repo.
You can find more info here. Hope this helps.
https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/creating-a-pull-request-from-a-fork
Thanks,
Bowen
from matchit.cpp.
Hi Bowen,
Here is a version with the toNullary helper function, please advise if you prefer this one.
Still, I could not merge the two versions of when/operator=, The general case takes a const reference and that cannot be changed because otherwise it does not bind to temporaries. The Id version takes a non-const reference and that cannot be changed either because get() and operator* of Ids are not const. So I had to keep them separate. The only way would be to have a const version of operator* or get() for Ids.
Best regards,
Hugo
from matchit.cpp.
Hi @hugoetchegoyen , you can try with universal reference:
template <typename T>
constexpr auto toNullary(T&& v) // added
{
if constexpr ( std::is_invocable_v<std::decay_t<T>> )
{
return v;
}
else
{
return expr(v);
}
}
from matchit.cpp.
Hi Bowen,
Great! The universal references solved it. If you think it's OK now I'll create a pull request.
Regards,
Hugo
from matchit.cpp.
PS sorry for the mistake, I did use std::decay_t in toNullary. Sent you an outdated file.
from matchit.cpp.
This is the updated file.
from matchit.cpp.
I already forked your repository, commited my changes, built the targets and ran unittest without errors, so it seems the changes don't break existing code. I'll create a pull request as soon as you agree.
from matchit.cpp.
LGTM. Feel free to create a pull request. We can continue our discussions there (if any).
And can you also add some unit tests to cover the new code paths?
And actually you can implement main functionality in toNullary and delegate expr to toNullary. Then "expr(Nullary const &v)" is no longer needed since you can use impl::toNullary instead.
from matchit.cpp.
@hugoetchegoyen I'm going to close this issue if there is no objection.
And I've added your name to the contributor section of the README. Thanks for your contribution.
from matchit.cpp.
Thank you very much Bowen.
Best regards,
Hugo
from matchit.cpp.
Related Issues (12)
- Id<T&&> works well after a mismatch?
- Add static_assert to make compile error message more readable.
- Found an interesting sample use case with entt.
- lambda parameters instead of id variables HOT 3
- CMakeLists.txt doesn't support installing the library HOT 1
- Add the library to vcpkg HOT 9
- Add the library to conan HOT 4
- Make Id a simpler wrapper of std::reference_wrapper. Context will hold the memory. HOT 1
- Add Id<Type&> Id<type&&> for mutation and rvalue support.
- Document Id<Type&> and Id<Type&&> usage. HOT 1
- Update documentation to clean up unnecessary usage of expr. HOT 5
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from matchit.cpp.