cppfront icon indicating copy to clipboard operation
cppfront copied to clipboard

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

Open filipsajdak opened this issue 3 years ago • 0 comments
trafficstars

Current implementation of inspect and is allows to inspect types. https://github.com/hsutter/cppfront/pull/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 https://github.com/hsutter/cppfront/pull/79

filipsajdak avatar Oct 20 '22 22:10 filipsajdak