Skip to content

Commit 19f284f

Browse files
committed
Add support for inspecting values (not just types), closes hsutter#79
Also cleaned up _alternative_ to use _type-id_ rather than _id-expression_.
1 parent 71ab934 commit 19f284f

12 files changed

+126
-11
lines changed

include/cpp2util.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -616,6 +616,23 @@ auto is( X const& x ) -> bool {
616616
}
617617

618618

619+
//-------------------------------------------------------------------------------------------------------------
620+
// Built-in is (values)
621+
//
622+
623+
inline constexpr auto is( auto const& x, auto const& value ) -> bool
624+
requires requires{ x == value; }
625+
{
626+
return x == value;
627+
}
628+
629+
inline constexpr auto is( auto const& x, auto const& value ) -> bool
630+
requires (!requires{ x == value; })
631+
{
632+
return false;
633+
}
634+
635+
619636
//-------------------------------------------------------------------------------------------------------------
620637
// Built-in as (partial)
621638
//
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
main: ()->int = {
2+
test(42);
3+
test(3.14);
4+
test(0);
5+
test(-42);
6+
test("xyzzy" as std::string);
7+
}
8+
9+
test: (x:_) = {
10+
forty_two := 42;
11+
std::cout << inspect x -> std::string {
12+
is 0 = "zero";
13+
is (forty_two) = "the answer";
14+
is int = "integer " + cpp2::to_string(x);
15+
is std::string = x;
16+
is _ = "(no match)";
17+
} << "\n";
18+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
the answer
2+
(no match)
3+
zero
4+
integer -42
5+
xyzzy

regression-tests/test-results/clang-12/pure2-inspect-values.cpp.output

Whitespace-only changes.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
the answer
2+
(no match)
3+
zero
4+
integer -42
5+
xyzzy

regression-tests/test-results/gcc-10/pure2-inspect-values.cpp.output

Whitespace-only changes.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
the answer
2+
(no match)
3+
zero
4+
integer -42
5+
xyzzy
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pure2-inspect-values.cpp
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// ----- Cpp2 support -----
2+
#define CPP2_USE_MODULES Yes
3+
#include "cpp2util.h"
4+
5+
6+
#line 1 "pure2-inspect-values.cpp2"
7+
[[nodiscard]] auto main() -> int;
8+
#line 9 "pure2-inspect-values.cpp2"
9+
auto test(auto const& x) -> void;
10+
11+
//=== Cpp2 definitions ==========================================================
12+
13+
#line 1 "pure2-inspect-values.cpp2"
14+
[[nodiscard]] auto main() -> int{
15+
test(42);
16+
test(3.14);
17+
test(0);
18+
test(-42);
19+
test(cpp2::as< std::string>("xyzzy"));
20+
}
21+
22+
auto test(auto const& x) -> void{
23+
auto forty_two { 42 };
24+
std::cout << [&] () -> std::string { auto&& __expr = x;
25+
if (cpp2::is(__expr, 0)) { if constexpr( requires{"zero";} ) if constexpr( std::is_convertible_v<CPP2_TYPEOF(("zero")),std::string> ) return "zero"; else return std::string{}; else return std::string{}; }
26+
else if (cpp2::is(__expr, (std::move(forty_two)))) { if constexpr( requires{"the answer";} ) if constexpr( std::is_convertible_v<CPP2_TYPEOF(("the answer")),std::string> ) return "the answer"; else return std::string{}; else return std::string{}; }
27+
else if (cpp2::is<int>(__expr)) { if constexpr( requires{"integer " + cpp2::to_string(x);} ) if constexpr( std::is_convertible_v<CPP2_TYPEOF(("integer " + cpp2::to_string(x))),std::string> ) return "integer " + cpp2::to_string(x); else return std::string{}; else return std::string{}; }
28+
else if (cpp2::is<std::string>(__expr)) { if constexpr( requires{x;} ) if constexpr( std::is_convertible_v<CPP2_TYPEOF((x)),std::string> ) return x; else return std::string{}; else return std::string{}; }
29+
else return "(no match)"; }
30+
()
31+
<< "\n";}
32+
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
pure2-inspect-values.cpp2... ok (all Cpp2, passes safety checks)
2+

0 commit comments

Comments
 (0)