cppfront
cppfront copied to clipboard
[FIX] Add missing overload for `is` function to handle enums
The current implementation does not have an overload for the is function to handle enums.
The below code compiles by cppfront but the result code fails to compile with the cpp1 compiler.
enum class lexeme : std::uint8_t {
hash,
};
to_string: (e:lexeme) -> auto = {
return inspect (e) -> std::string {
is lexeme::hash = "hash";
is _ = "INTERNAL_ERROR";
};
}
cppfront result
[[nodiscard]] auto to_string(cpp2::in<lexeme> e) -> auto{
return [&] () -> std::string { auto&& __expr = (e);
if (cpp2::is<lexeme::hash>(__expr)) { if constexpr( requires{"hash";} ) if constexpr( std::is_convertible_v<CPP2_TYPEOF("hash"),std::string> ) return "hash"; else return std::string{}; else return std::string{}; }
else return "INTERNAL_ERROR"; }
()
; }
Unfortunately, there is no overload that can match using an enum value. This change provides additional overload that works with enums - it checks if the value is an enum type and if the compared value is the same type as the enum value provided.
Working prototype: https://godbolt.org/z/1c4o8qG6P
Close https://github.com/hsutter/cppfront/issues/73
I have made a small change to the is function overload to work with various of enums. You can now compare values of two different types of enums and have a false as a return - it will compile in inspect like below:
v := lexeme::hash;
std::cout << inspect v -> std::string {
is lexeme::hash = "original lexeme";
is lexeme2::hash = "fake lexeme2";
is _ = "don't know";
} << std::endl;
and will match the proper alternative. Of course, it will also work for the lexeme2::hash value.
I am closing it as it is also covered by https://github.com/hsutter/cppfront/pull/79