Skip to content

[SUGGESTION] Add inspection of std::variant and std::optional values #86

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

Current implementation of inspect and is allows to inspect types. #79 add inspection capabilities to check values of variables (what allows to replace cpp1 switches).

This change introduce possibility to inspect values stored in std::variant and std::optional what make given code work:

v : std::variant<lexeme, lexeme2, std::string, int, double, char, my_struct> = lexeme2::hash;

std::cout << inspect v -> std::string {
    is lexeme::hash  = "original lexeme";
    is lexeme2::hash = "fake lexeme2";
    is "hash"        = "it is string 'hash'";
    is 42            = "the answer is 42";
    is 1.23          = "the answer is 1.23";
    is 'a'           = "this is 'a'";
    is _             = "don't know";
} << std::endl;

or you can use it as if statement with std::optional

o : std::optional<std::string> = "hash";

if o is "hash" {
    std::cout << "Optional has 'hash' value" << std::endl;
} else {
    std::cout << "There is something else" << std::endl;
}

or std::variant

if v is 1.23 {
  std::cout << "This is 1.23" << std::end;
}

This PR is based on #79

@filipsajdak filipsajdak force-pushed the fsajdak-inspect-variant-values branch from 8f3ecfd to dd7d4a3 Compare October 30, 2022 19:59
@filipsajdak filipsajdak force-pushed the fsajdak-inspect-variant-values branch 2 times, most recently from 073a521 to e4b0db9 Compare November 29, 2022 21:33
@filipsajdak
Copy link
Contributor Author

Rebase to main. Make is() functions constexpr

@filipsajdak
Copy link
Contributor Author

Might require #90 for some use cases.

@filipsajdak filipsajdak force-pushed the fsajdak-inspect-variant-values branch from e4b0db9 to 58a2727 Compare December 6, 2022 22:13
@filipsajdak filipsajdak force-pushed the fsajdak-inspect-variant-values branch from 58a2727 to 95359ca Compare December 15, 2022 22:53
@hsutter
Copy link
Owner

hsutter commented Dec 17, 2022

Might require #90 for some use cases.

Should these be merged? Should all the is/as PRs be merged (also #79, #106, #108)? (Not suggesting, just asking. As I look through them I'm trying to grok the overlap.)

@fecjanky
Copy link

Conceptually can we make std::optional<T> to be isomorphic to std::variant<std::monostate,T> from the cpp2 language/library point of view as these types are isomorphic mathematically as well? This would definitely simplify things on multiple levels.

@filipsajdak filipsajdak force-pushed the fsajdak-inspect-variant-values branch from 95359ca to a9f26b4 Compare December 20, 2022 23:35
@hsutter
Copy link
Owner

hsutter commented Dec 21, 2022

Thanks! About half of this (parsing and literals) seems to be duplicated from #79, so I'll focus on the variant, optional, and any parts... more soon...

@hsutter
Copy link
Owner

hsutter commented Dec 21, 2022

OK, half of this which wasn't about variant/optional/any was merged in the commit c77de70 that closed #79, and I'm going to apply the rest for those three standard types in a separate commit to follow the pattern of commit c77de70 to use the two-parameter "is value" pattern.

Thanks again!

@hsutter
Copy link
Owner

hsutter commented Dec 21, 2022

I've now added this with 2f37277 - thanks again.

@hsutter hsutter closed this Dec 21, 2022
@filipsajdak filipsajdak deleted the fsajdak-inspect-variant-values branch December 22, 2022 10:42
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

Successfully merging this pull request may close these issues.

3 participants