FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

[FIX] Add missing overload for `is` function to handle enums by filipsajdak · Pull Request #74 · hsutter/cppfront · GitHub

Repository navigation

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

Closed
filipsajdak wants to merge 1 commit into
hsutter:mainfrom
filipsajdak:fsajdak-fix-is-for-enums
Closed

filipsajdak wants to merge 1 commit into
hsutter:mainfrom
filipsajdak:fsajdak-fix-is-for-enums

Conversation

Copy link
Copy Markdown
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 force-pushed the fsajdak-fix-is-for-enums branch from 6c605ef to 6af7b2e Compare October 14, 2022 20:12

Copy link
Copy Markdown
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.

Copy link
Copy Markdown
Contributor Author

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

filipsajdak deleted the fsajdak-fix-is-for-enums branch October 18, 2022 09:01
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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


Back | FazBrowse Home | New Git URL