Skip to content

[SUGGESTION] Add inspect is() matchers for templates #108

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

Merged

Conversation

filipsajdak
Copy link
Contributor

Thanks to other experiments I have added the possibility to inspect if a type is a template. That will allow generic code to check if we deal with std::vector, std::array, std::unique_ptr, std::variant, or std::initializer_list (working prototype with more templates I have checked is here: https://godbolt.org/z/ddMdq5W4T).

This change makes below code possible:

#include <vector>
#include <array>
#include <string>

template <typename A, typename B>
struct my_type {};

fun: (v : _) -> std::string = {
    return inspect v -> std::string {
        is std::vector  = "std::vector";
        is std::array   = "std::array";
        is std::variant = "std::variant";
        is my_type      = "my_type";
        is _ = "unknown";
    };
}

main: () -> int = {
    vec : std::vector<int> = (1,2,3);
    arr : std::array<int,4> = (1,2,3,4);
    var : std::variant<int, double, std::string> = ("cpp2 rulez");
    myt : my_type<int, double> = ();

    std::cout << "inspected vec : (fun(vec))$" << std::endl;
    std::cout << "inspected arr : (fun(arr))$" << std::endl;
    std::cout << "inspected var : (fun(var))$" << std::endl;
    std::cout << "inspected myt : (fun(myt))$" << std::endl;
}

Limitations

The current solution is based on template template arguments. The current solution will work with templates that match:

  • template <typename...> class C;, or
  • template <typename,auto> class C - this is needed to handle std::array

Unfortunately, there is no way of having a universal match for type and non-type parameters in a variadic way. If needed more matches there would be a need to create more combinations of template template arguments.

@filipsajdak filipsajdak force-pushed the fsajdak-inspect-add-matchers-for-templates branch from 4dd4dab to e1ea91b Compare November 5, 2022 09:29
@filipsajdak filipsajdak force-pushed the fsajdak-inspect-add-matchers-for-templates branch 2 times, most recently from ffffd80 to 235628a Compare December 3, 2022 16:35
@filipsajdak filipsajdak force-pushed the fsajdak-inspect-add-matchers-for-templates branch from 235628a to e1b1606 Compare December 6, 2022 21:53
@filipsajdak filipsajdak force-pushed the fsajdak-inspect-add-matchers-for-templates branch from e1b1606 to 36e88c6 Compare December 15, 2022 22:59
@filipsajdak filipsajdak force-pushed the fsajdak-inspect-add-matchers-for-templates branch from 36e88c6 to b2a4714 Compare December 20, 2022 23:11
@filipsajdak filipsajdak force-pushed the fsajdak-inspect-add-matchers-for-templates branch from b2a4714 to 83b93e0 Compare December 22, 2022 21:52
@filipsajdak
Copy link
Contributor Author

rebased to main

@hsutter
Copy link
Owner

hsutter commented Dec 25, 2022

Thanks! I think this sets a record for the fastest non-trivial cppfront PR review yet... I tried the code in my own branch and it just worked on the first try with all three compilers. Groovy. Merging...

@hsutter hsutter merged commit 8665efd into hsutter:main Dec 25, 2022
@hsutter
Copy link
Owner

hsutter commented Dec 25, 2022

BTW for other readers: One reason this could be reviewed so quickly is because the P2392 design paper for inspect/is/as called out matching templates as one of the intended matching rules (section 2.1):

Otherwise, if C is a template-name, then x is C is true if x is a specialization of C, else false.

... which just hadn't been implemented yet in cppfront, so this extension PR is on plan. Thanks @filipsajdak

@filipsajdak
Copy link
Contributor Author

Thank you. I proposed it based on the paper - https://wg21.link/p2392

hsutter added a commit that referenced this pull request Dec 25, 2022
@filipsajdak filipsajdak deleted the fsajdak-inspect-add-matchers-for-templates branch December 27, 2022 18:27
Azmah-Bad pushed a commit to Azmah-Bad/cppfront that referenced this pull request Feb 24, 2023
…atchers-for-templates

[SUGGESTION] Add inspect `is()` matchers for templates
Azmah-Bad pushed a commit to Azmah-Bad/cppfront that referenced this pull request Feb 24, 2023
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.

2 participants