Skip to content

[FIX] Add missing overload for is function to handle enums #74

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

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 #73

@filipsajdak filipsajdak force-pushed the fsajdak-fix-is-for-enums branch from 6c605ef to 6af7b2e Compare October 14, 2022 20:12
@filipsajdak
Copy link
Contributor Author

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.

@filipsajdak
Copy link
Contributor Author

I am closing it as it is also covered by #79

@filipsajdak filipsajdak deleted the fsajdak-fix-is-for-enums branch October 18, 2022 09:01
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.

[BUG] Unable to use enums in inspect expressions - no matching function for call to 'is'
1 participant