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

lambda parameters instead of id variables #68

Closed
GunpowderGuy opened this issue Oct 19, 2021 · 3 comments
Closed

lambda parameters instead of id variables #68

GunpowderGuy opened this issue Oct 19, 2021 · 3 comments
Labels
good first issue Good for newcomers

Comments

@GunpowderGuy
Copy link

GunpowderGuy commented Oct 19, 2021

Could having to use id variables be replaced with lambda parameters?
eg: instead of this

Id<string> text;
match(variant)(
pattern | as<string>(text) = [&] {
            std::cout << "Text message: " << *text << std::endl;
        })

this

match(variant)(
pattern | as<string> = [](const string& text) {
            std::cout << "Text message: " << text << std::endl;
        })
@GunpowderGuy
Copy link
Author

GunpowderGuy commented Oct 24, 2021

@BowenFu If that isn't possible i propose to add an special match for variants that works as syntactic sugar for visit, with functionality similar to the "overloaded" type defined here : https://en.cppreference.com/w/cpp/utility/variant/visit
That would also allow for the thoroughness of cases to be checked which i gather cant be done for general pattern matching without language support

@BowenFu
Copy link
Owner

BowenFu commented Oct 25, 2021

@GunpowderGuy Will this meet your requirements?
https://godbolt.org/z/oo38T9xK3

    match(x)(
        visit<std::string>(
            [](const std::string& text) {
                std::cout << "Text message: " << text << std::endl;
            }),
        visit<int32_t>(
            [](const int32_t num) {
                std::cout << "Number: " << num << std::endl;
            })
        );

Note: We need to store the value instead ref in PatternPair to fix the memory issue.
https://godbolt.org/z/Yrhj8T7PM should be the best we can use with current release.

    match(x)(
        visit<std::string>(
            [](const std::string& text) {
                std::cout << "Text message: " << text << std::endl;
            }) = []{},
        visit<int32_t>(
            [](const int32_t num) {
                std::cout << "Number: " << num << std::endl;
            }) = []{}
        );

@BowenFu BowenFu added the good first issue Good for newcomers label Oct 26, 2021
BowenFu pushed a commit that referenced this issue Oct 27, 2021
BowenFu pushed a commit that referenced this issue Oct 27, 2021
@BowenFu BowenFu reopened this Oct 27, 2021
@BowenFu
Copy link
Owner

BowenFu commented Oct 27, 2021

The memory issue has been fixed.

@BowenFu BowenFu closed this as completed Nov 24, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

2 participants