Coder Social home page Coder Social logo

Comments (16)

BowenFu avatar BowenFu commented on July 30, 2024

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.

hugoetchegoyen avatar hugoetchegoyen commented on July 30, 2024

from matchit.cpp.

hugoetchegoyen avatar hugoetchegoyen commented on July 30, 2024

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.

BowenFu avatar BowenFu commented on July 30, 2024

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

hugoetchegoyen avatar hugoetchegoyen commented on July 30, 2024

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.

hugoetchegoyen avatar hugoetchegoyen commented on July 30, 2024

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

matchit2.txt

from matchit.cpp.

BowenFu avatar BowenFu commented on July 30, 2024

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

hugoetchegoyen avatar hugoetchegoyen commented on July 30, 2024

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

matchit3.txt

from matchit.cpp.

BowenFu avatar BowenFu commented on July 30, 2024

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.

hugoetchegoyen avatar hugoetchegoyen commented on July 30, 2024

Hi Bowen,

Great! The universal references solved it. If you think it's OK now I'll create a pull request.

Regards,
Hugo

matchit4.txt

from matchit.cpp.

hugoetchegoyen avatar hugoetchegoyen commented on July 30, 2024

PS sorry for the mistake, I did use std::decay_t in toNullary. Sent you an outdated file.

from matchit.cpp.

hugoetchegoyen avatar hugoetchegoyen commented on July 30, 2024

This is the updated file.

matchit4.txt

from matchit.cpp.

hugoetchegoyen avatar hugoetchegoyen commented on July 30, 2024

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.

BowenFu avatar BowenFu commented on July 30, 2024

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.

BowenFu avatar BowenFu commented on July 30, 2024

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

hugoetchegoyen avatar hugoetchegoyen commented on July 30, 2024

Thank you very much Bowen.
Best regards,
Hugo

from matchit.cpp.

Related Issues (12)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.