-
-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathConditional-if-let-Expressions.cpp
40 lines (34 loc) · 1.13 KB
/
Conditional-if-let-Expressions.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include "matchit.h"
#include <iostream>
#include <optional>
template <typename V, typename E>
using Result = std::variant<V, E>;
Result<uint8_t, std::exception> parse(std::string_view) { return uint8_t{34U}; }
int32_t main()
{
auto const favorite_color = std::optional<std::string>{};
auto const is_tuesday = false;
Result<uint8_t, std::exception> const age = parse("34");
using namespace matchit;
Id<std::string> color;
match(favorite_color)(
pattern | some(color) =
[&]
{
return "Using your favorite color, " + *color +
", as the background";
},
pattern | _ | when(is_tuesday) = "Tuesday is green day!",
pattern | _ =
[&]
{
Id<uint8_t> age_;
return match(age)(pattern | as<uint8_t>(age_) | when(age_ > 30) =
"Using purple as the background color",
pattern | as<uint8_t>(age_) =
"Using orange as the background color",
pattern | _ =
"Using blue as the background color");
});
return 0;
}